Fhernd icon

funciones_callback_en_linea.py

Fhernd | PRO | 11/18/18 11:11:35 AM UTC | 0 ⭐ | 3699 👁️ | Never ⏰ | []
Python |

1.02 KB

|

None

|

0 👍

/

0 👎

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()

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎