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