python11—生成器2
一、生产者消费者模型
#协程,单线程中的并发
1、def consumer(name): #生成器函数
print('我是【%s】,准备吃包子了 %name')
while True:
baozi=yield #(next)第一步输出yield值,(send)第二步接受值继续执行到下一个yield
time.sleep(0.1)
print('%s 把%s吃掉了',%(name,baozi))
def product():
c1=consumer('xiaogao') #生成器对象
c2=consumer('xmsb') #生成器对象
c1.__next__()
c2.__next__()
for i in rang(10):
time.sleep(0.1)
c1.send('肉馅包子%s' %i) #传给baozi=yield
c2.send('肉馅包子%s' %i) #传给baozi=yield
运行:product()
浙公网安备 33010602011771号