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:
#print("product %s remained" % m) m += i # increased every time print("%s fruits increased, we have %s now in the basket" %(i, m)) m = yield from consume(m) pass
loop = asyncio.get_event_loop()
print("%s fruits initilized" % m)