一、效果截屏spa
代碼以下:3d
1 import time 2 3 def consumer(name): 4 print('%s 開始買手機' %name) 5 while True: 6 baozi=yield 7 print('\033[31;1m手機[%s] 造好了,被[%s] 買了!\033[0m' %(baozi,name)) 8 9 c = consumer('小明') 10 c.__next__() # __next__是隻喚醒 11 12 b1='小米Max 10' 13 c.send(b1) #send 是喚醒 yield,同時給yield 傳入數據。__next__是隻喚醒 14 15 def producer(name): 16 c=consumer('A') 17 c2=consumer('B') 18 c.__next__() 19 c2.__next__() 20 print('開始造手機了') 21 for i in range(10): 22 time.sleep(1) 23 print('\033[32;1m造了2個手機\033[0m') 24 c.send(i) 25 c2.send(i) 26 27 producer('alex')