python生成器異步使用
阿新 • • 發佈:2018-08-31
tar one spa pass lee iteration thread import 運行
import dis,time # 反匯編 import threading def request(): print(‘start request‘) v = yield print(v) def target(func): try: s = time.time() print(‘start target‘) c = func() c.send(None) time.sleep(7) c.send(f‘done: {time.time()-s}‘)except StopIteration: pass def server(func): threading.Thread(target=target,args=(func,)).start() server(request) for i in range(10): time.sleep(1) print(i)
運行結果
>>> start target start request 0 1 2 3 4 5 done: 7.004955053329468 6 7 8 9
python生成器異步使用