Created
November 18, 2018 12:11
-
-
Save Fhernd/f02c048fd534e457a683d04605a0d1ee to your computer and use it in GitHub Desktop.
Uso de funciones en-línea. Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from queue import Queue | |
from functools import wraps | |
def aplicar_asincronismo(funcion, args, *, callback): | |
resultado = funcion(*args) | |
callback(resultado) | |
class Asincronica: | |
def __init__(self, funcion, args): | |
self.funcion = funcion | |
self.args = args | |
def asincronismo(funcion): | |
@wraps(funcion) | |
def envoltura(*args): | |
f = funcion(*args) | |
cola = Queue() | |
cola.put(None) | |
while True: | |
resultado = cola.get() | |
try: | |
a = f.send(resultado) | |
aplicar_asincronismo(a.funcion, a.args, callback=cola.put) | |
except StopIteration: | |
break | |
return envoltura | |
def sumar(x, y): | |
return x + y | |
@asincronismo | |
def prueba(): | |
r = yield Asincronica(sumar, (2, 3)) | |
print(r) | |
r = yield Asincronica(sumar, ('Python', ' 3.x')) | |
print(r) | |
for n in range(10): | |
r = yield Asincronica(sumar, (n, n + 1)) | |
print(r) | |
print('Cierre') | |
prueba() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment