Python3 生成器实现单线程并发

 1 # Author = Johnston
 2 import time
 3 def consumer(name):
 4     print("%s准备吃包子了。。。" %name)
 5     while True:
 6         baozi = yield
 7         print("%s吃了%s个包子" %(name,baozi))
 8 
 9 def productor():
10     print("我准备做包子了。。。")
11     a = consumer("A")
12     b = consumer("B")
13     a.__next__()
14     b.__next__()
15     for i in range(10):
16         time.sleep(1)
17         print("我做了两个包子")
18         a.send(i+1)
19         b.send(i+1)
20 
21 productor()
View Code

 

posted @ 2017-10-04 02:40  游首好咸  阅读(166)  评论(0)    收藏  举报