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()

 

posted @ 2019-09-17 16:09  82年的加多宝  阅读(111)  评论(0编辑  收藏  举报