Why is asyncio queue await get() blocking?
You need to start the consumer and the producer in parallel, e.g. defining main
like this:
async def main():
queue = asyncio.Queue()
await asyncio.gather(consumer(queue), producer(queue, 1))
If for some reason you can't use gather
, then you can do (the equivalent of) this:
async def main():
queue = asyncio.Queue()
asyncio.create_task(consumer(queue))
asyncio.create_task(producer(queue, 1))
await asyncio.sleep(100) # what your program actually does
Why isn't
await queue.get()
yielding control back to the event loop so that the producer coroutine can run which will populate the queue so thatqueue.get()
can return.
await queue.get()
is yielding control back to the event loop. But await means wait, so when your main
coroutine says await consumer(queue)
, that means "resume me once consumer(queue)
has completed." Since consumer(queue)
is itself waiting for someone to produce something, you have a classic case of deadlock.
Reversing the order works only because your producer is one-shot, so it immediately returns to the caller. If your producer happened to await an external source (such as a socket), you would have a deadlock there as well. Starting them in parallel avoids the deadlock regardless of how producer
and consumer
are written.