result = func(*args)#有可能中断 callback(result) classAsync: def__init__(self,func,args): self.func = func self.args = args definlined_async(func): @wraps(func) defwrapper(*args): f = func(*args)#get a generator result_queue = Queue() result_queue.append(None) whileTrue: result = result_queue.get()#wait the result to be generated try: a = f.send(result) apply_async(a.func,a.args,callback = result_queue.put)# callback will put a.func(*a.args) in result_queue except StopIteration: break return wrapper defadd(x,y): import time time.sleep(1) print('done') return x+y @inlined_async deftest(): r = yield Async(add,(2,3))#generate the function and args you want async_apply to compute print(r) r = yield Async(add,('hello','world')) print(r) for n in range(15): r = yield Async(add,(n,n))#async to compute:异步计算 print(r)#print what we get print('Goodbyy')