liveBook · Manning

Queue.get

As explained in Python Concurrency with asyncio

queue.get is an awaitable queue method that waits asynchronously until an item is available and then removes and returns it.

queue.get is an awaitable queue method: it waits asynchronously until an item is available, then removes and returns that item. Unlike get_nowait(), it lets a worker remain available without repeatedly checking whether the queue contains work. section 12.1

A typical worker loops forever, awaits queue.get(), processes the returned item, and calls queue.task_done() when processing finishes. Multiple worker tasks can therefore share work from one queue. section 12.1

Supermarket checkout workers

In a supermarket checkout simulation, each cashier awaits the next customer with `customer = await queue.get()`. The cashier checks out that customer's products, while other cashier tasks independently retrieve and process other customers. section 12.1

How Python Concurrency with asyncio covers it

How deep it goes
Demonstrates awaited retrieval in a worker loop and contrasts it with get_nowait(), without detailing queue internals.
Examples
Uses a supermarket checkout simulation in which cashier tasks retrieve customer objects and process their products.
What it assumes
Assumes familiarity with coroutine functions, await, and asynchronous tasks.
What you can do after
Apply it via asyncio.Queue to build workers that asynchronously receive and process queued work.
How it connects
Connects queue.get with asyncio queues, coroutine functions, worker tasks, task_done(), join(), and get_nowait().

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.