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
