利用yield將異步回調同步化


《python cookbook》上這段代碼利用yield將異步回調同步化,這跟tornado的@gen.coroutine用法好像,感覺tornado的gen.coroutine裝飾器背后可能就是這個原理,將被裝飾函數的yield逐步遍歷並等待被裝飾函數下次yield出,若收到生成器結束的異常,則裝飾器函數也退出。同步化的編程方式還有一個特點就是整個流程全程都是可見的,不會有上下文環境訪問不到


from queue import Queue
from functools import wraps


def apply_async(func, args, *, callback):
    # Compute the result
    result = func(*args)

    # Invoke the callback with the result
    callback(result)

class Async:
    def __init__(self, func, args):
        self.func = func
        self.args = args

def inlined_async(func):
    @wraps(func)
    def wrapper(*args):
        f = func(*args)
        result_queue = Queue()
        result_queue.put(None)
        while True:
            result = result_queue.get()
            try:
                ## 注意,這里不僅發送消息給生成器,並且等待生成器返回值
                a = f.send(result)
                apply_async(a.func, a.args, callback=result_queue.put)
            except StopIteration:
                break
    return wrapper




def add(x, y):
    return x + y

@inlined_async
def test():
    r = yield Async(add, (2, 3))
    print(r)
    r = yield Async(add, ('hello', 'world'))
    print(r)
    for n in range(10):
        r = yield Async(add, (n, n))
        print(r)
    print('Goodbye')


def main():
    test()


main()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM