liveBook · Manning

Asyncio.Queue

As explained in Python Concurrency with asyncio

asyncio.Queue is a Python asyncio class implementing an asynchronous first-in-first-out queue with optional capacity limits.

asyncio.Queue coordinates asynchronous workers through a first-in-first-out line of work items. Producers add items, while worker tasks retrieve them and process them concurrently without blocking the event loop. section 12.1

The queue supports two styles of retrieval and insertion:

  • Use get_nowait() and put_nowait() for immediate operations; an empty queue or a full bounded queue raises an error. section 12.1
  • Call task_done() after processing each retrieved item so queue.join() can track when all unfinished work is complete. section 12.1

Supermarket checkout

Represent customers as work items and cashiers as worker tasks. Each cashier awaits a customer, processes its products, and sleeps for each product's checkout time. Several cashiers can therefore handle different customers while the queue preserves arrival order. section 12.1

How Python Concurrency with asyncio covers it

How deep it goes
Explains FIFO processing, immediate and awaited methods, capacity limits, and unfinished-task tracking through a runnable scenario.
Examples
Uses customers, products, cashiers, checkout durations, bounded capacity, empty retrieval, and interleaved checkout output.
What you can do after
Apply it via Python asyncio to build asynchronous worker pipelines with optional queue capacity and completion tracking.
How it connects
Connects asynchronous queues with worker tasks, coroutines, asyncio.sleep, and asyncio.run.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.