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()
 

转载于:https://www.cnblogs.com/POP-w/p/7489988.html

你可能感兴趣的:(php)