闲得蛋疼,我这个高级销售经理写一个吃水果的消费生成者程序给你们看看。
Topic source妈蛋上面写的有问题,重新改了一下。--->
author = "Kevin Zhang@Huawei"
import random import asyncio
m = 0 count = 1 Max = 100
@asyncio.coroutine def consume(x): global m, count, Max m = x i = random.randint(1,3) if (x -i) > 0: # if it's enough to eat yield from asyncio.sleep(1.0) m = x - i # eat random ones from basket m; print("I have eaten %s, %s remained in the basket..." %(i,m)) if count < Max: count += 1 yield from produce(m)# control changed back to producer for fruit re-making else: return
@asyncio.coroutine
def produce(x):
global m, count
m = x
i = random.randint(0,3)
m += i # increased every time
print("%s fruits increased, we have %s now in the basket" %(i, m))
yield from consume(m)
pass
loop = asyncio.get_event_loop() loop.run_until_complete(produce(m)) loop.close()
- 1
科子六六
author = "Kevin Zhang@Huawei"
import random import asyncio
m = 0
@asyncio.coroutine def consume(x): global m m = x i = random.randint(1,3) while (x -i) > 0: # if it's enough to eat yield from asyncio.sleep(5.0) m = x - i # eat random ones from basket m; print("I have eaten %s, %s remained in the basket..." %(i,m)) m = yield from produce(m)# control changed back to producer for fruit re-making m = yield from produce(m)# control changed back to producer for fruit re-making pass
@asyncio.coroutine def produce(x): global m m = x i = random.randint(0,3) while True:
loop = asyncio.get_event_loop()
print("%s fruits initilized" % m)
loop.run_until_complete(produce(m)) loop.close()