Skip to content

Instantly share code, notes, and snippets.

@akrylysov
Last active January 10, 2023 17:04
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save akrylysov/ebab39ca9dafd292916e to your computer and use it in GitHub Desktop.
Save akrylysov/ebab39ca9dafd292916e to your computer and use it in GitHub Desktop.
Python 3 asyncio basic producer / consumer example
import asyncio
import random
q = asyncio.Queue()
async def producer(num):
while True:
await q.put(num + random.random())
await asyncio.sleep(random.random())
async def consumer(num):
while True:
value = await q.get()
print('Consumed', num, value)
loop = asyncio.get_event_loop()
for i in range(6):
loop.create_task(producer(i))
for i in range(3):
loop.create_task(consumer(i))
loop.run_forever()
@lostcoaster
Copy link

lostcoaster commented Jan 23, 2018

@NNTin
await puts the coroutine into the only thread of the event loop. So it will block the producer, as well as all other producers.

You can use asyncio.run_in_executor to move the big calculation to other threads, e.g.

def bigcalculation():
  ...

await q.put(await asyncio.run_in_executor(None, bigcalculation))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment