python生成器异步使用
2021-07-09 17:07
标签:exce tar range yield pytho get import start 异步 运行结果 python生成器异步使用 标签:exce tar range yield pytho get import start 异步 原文地址:https://www.cnblogs.com/tangpg/p/9565526.htmlimport 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 断言大全