liveBook · Manning

Queue.put

As explained in Python Concurrency with asyncio

queue.put is an awaitable queue method that waits asynchronously until capacity is available and then inserts an item.

queue.put is an awaitable method on an asyncio queue. It waits asynchronously when a bounded queue is full, then inserts the item when capacity becomes available. section 12.1 section 12.1.1

Use queue.put when producers should cooperate with consumers instead of failing immediately:

  • Create a queue with an optional maximum size, then await queue.put(item) to add work. section 12.1
  • Let workers await queue.get() while producers await queue.put(); a full queue naturally applies backpressure. section 12.1 section 12.1.1
  • Prefer put over put_nowait when temporary fullness should make the producer wait rather than raise an exception. section 12.1 section 12.1.1

Queueing web orders

An aiohttp POST handler can await order_queue.put(order) and immediately return that the order was placed. Background workers later await queue.get(), process the order, and call queue.task_done(). section 12.1.1

How Python Concurrency with asyncio covers it

How deep it goes
Explains awaitable insertion, bounded-queue waiting, and worker coordination, but does not detail queue implementation internals.
Examples
Uses supermarket customers and products, then applies insertion to an aiohttp order-processing endpoint.
What it assumes
Assumes basic Python coroutines and await syntax while building queue and worker concepts through concrete examples.
What you can do after
Operate asyncio.Queue producers that enqueue work asynchronously and coordinate with bounded worker pools.
How it connects
Connects queue.put with asyncio.Queue, queue.get, queue.task_done, worker tasks, and aiohttp background processing.

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.