import threading
import time
gen = None
def longIO():
def run():
print('开始耗时操作')
time.sleep(5)
try:
global gen
gen.send('brotech is good')
except StopIteration as e:
pass
print('结束耗时操作')
threading.Thread(target=run).start()
def genCoroutine(func):
def wrapper():
global gen
gen = func()
next(gen)
return wrapper
@genCoroutine
def reqA():
print('开始处理reqA')
res = yield longIO()
print('接收到longIo的响应数据:', res)
print('结束处理reqA')
def reqB():
print('开始处理B')
print('结束处理B')
def main():
# global gen
# gen = reqA()
# next(gen)
reqA()
reqB()
if __name__ == '__main__':
main()
异步-协程(装饰器)
最新推荐文章于 2024-01-25 20:00:00 发布