Python 生产者和消费者模型

import time
def consumer(name):
print ("%s consumer one product" %name)
while True:
baozi = yield
print ("baozi[%s] bei [%s] chile" %(baozi,name))

def producter(name):
c = consumer('A')
c2 = consumer('B')
c.__next__()
c2.__next__()
print ("begin to make baozi")
for i in range(10):
time.sleep(1)
print ("make two baozi")
c.send(i)
c2.send(i)

producter('alex')

结果:

A consumer one product
B consumer one product
begin to make baozi
make two baozi
baozi[0] bei [A] chile
baozi[0] bei [B] chile
make two baozi
baozi[1] bei [A] chile
baozi[1] bei [B] chile
make two baozi
baozi[2] bei [A] chile
baozi[2] bei [B] chile
make two baozi
baozi[3] bei [A] chile
baozi[3] bei [B] chile
make two baozi
baozi[4] bei [A] chile
baozi[4] bei [B] chile
make two baozi
baozi[5] bei [A] chile
baozi[5] bei [B] chile
make two baozi
baozi[6] bei [A] chile
baozi[6] bei [B] chile
make two baozi
baozi[7] bei [A] chile
baozi[7] bei [B] chile
make two baozi
baozi[8] bei [A] chile
baozi[8] bei [B] chile
make two baozi
baozi[9] bei [A] chile
baozi[9] bei [B] chile

 结论:c,c2返回为迭代器,故而consumer为生产者,注意用断点调试的方法来查看。

 

转载于:https://www.cnblogs.com/python-study/p/5463681.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值