import time def consumer(name): print("%s开始吃包子了"%name) while True: ret = yield time.sleep(1) print("%s吃了%d个包子"%(name,ret)) def producer(): c = consumer("liaoboshi") # c 代表一个生成器 c1 = consumer("tom") c.__next__() #.__next___()或者.send()执行一次,返回一个结果,光标就会停在yield的后面 c1.__next__() for i in range(1,10): time.sleep(1) print("做了%d个包子"%i) c1.send(i) c.send(i) # .send(self, value)和.__next__(self) 一样,value会传回给上面yield的ret producer()