Python-生成器实现简单的\"生产者消费者\"模型
一、使用生成器实现简单的生产者消费者模型,
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‘)
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16