tengyunjiawucom 2020-03-26
1、效果截屏
代码如下:
import time def consumer(name): print(‘%s 开始买手机‘ %name) while True: baozi=yield print(‘\033[31;1m手机[%s] 造好了,被[%s] 买了!\033[0m‘ %(baozi,name)) c = consumer(‘小明‘) c.__next__() # __next__是只唤醒 b1=‘小米Max 10‘ c.send(b1) #send 是唤醒 yield,同时给yield 传入数据。__next__是只唤醒 def producer(name): c=consumer(‘A‘) c2=consumer(‘B‘) c.__next__() c2.__next__() print(‘开始造手机了‘) for i in range(10): time.sleep(1) print(‘\033[32;1m造了2个手机\033[0m‘) c.send(i) c2.send(i) producer(‘alex‘)