python生成器异步使用

2021-07-09 17:07

阅读:563

标签:exce   tar   range   yield   pytho   get   import   start   异步   

 

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(fdone: {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生成器异步使用

标签:exce   tar   range   yield   pytho   get   import   start   异步   

原文地址:https://www.cnblogs.com/tangpg/p/9565526.html


评论


亲,登录后才可以留言!