# SuperFastPython.com # example of producer/consumer connected via a queue from random import random import asyncio # coroutine to generate work async def producer(queue): print('Producer: Running') # generate work for _ in range(10): # generate a value value = random() # suspend to simulate work await asyncio.sleep(value) # add to the queue await queue.put(value) # send an all done signal await queue.put(None) print('Producer: Done') # coroutine to consume work async def consumer(queue): print('Consumer: Running') # consume work while True: # get a unit of work item = await queue.get() # check for stop signal if item is None: break # report print(f'>got {item}') # all done print('Consumer: Done') # entry point coroutine async def main(): # create the shared queue queue = asyncio.Queue() # run the producer and consumers await asyncio.gather( producer(queue), consumer(queue)) # start the asyncio program asyncio.run(main())