首页 > 其他 > 详细

yield实现异步

时间:2017-09-07 15:49:49      阅读:273      评论:0      收藏:0      [点我收藏+]
def cash_out(amount):
while amount >0:
amount-=1
yield 1
print(‘get again %s‘ %amount)
atm=cash_out(5)
print(type(atm))
print(atm.next())
print(234234)
print(atm.next())



import time
def consumer(name):
print("%s 准备吃包子啦!" %name)
while 1:
baozi = yield
print("包子[%s]来了,被[%s]吃了!" %(baozi, name))

def producer():
c = consumer(‘A‘)
c2 = consumer(‘B‘)
c.next()
c2.next()
print("老子开始准备做包子啦!")
for i in range(10):
time.sleep(1)
print("做了2个包子!")
c.send(i)
c2.send(i)
producer()
 

yield实现异步

原文:http://www.cnblogs.com/POP-w/p/7489988.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!