day19生产者消费模型yield

生产者消费:

 1 def consumer(name):
 2     print("我是%s,我准备吃包子!" %name)
 3     while True:
 4         baozi = yield
 5         print("%s 很开心的把【%s】吃掉了" %(name,baozi))
 6 
 7 def producer():
 8     c = consumer("alex")
 9     c1 = consumer("tom")
10     next(c)
11     next(c1)
12     for i in range(10):
13         if i / 2 ==0:
14             c.send("包子%s" %i)
15         else:
16             c1.send("包子%s" %i)
17 producer()
原文地址:https://www.cnblogs.com/Tang-Yuan/p/9804603.html