生產者消費:spa
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()