Python3 生成器实现单线程并发

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于Python3 生成器实现单线程并发的主要内容,如果未能解决你的问题,请参考以下文章