yield实现异步

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()
 
posted @ 2017-09-07 15:29  pop_PY  阅读(220)  评论(0编辑  收藏  举报