Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement