liveBook · Manning

Asyncio.PriorityQueue

As explained in Python Concurrency with asyncio

asyncio.PriorityQueue is an asyncio queue class that retrieves enqueued items in priority order.

asyncio.PriorityQueue lets asynchronous workers process urgent work before older, less important work. It retrieves the smallest priority value first, so lower numbers represent higher priority. section 12.2

Represent queued work with an ordering that matches the scheduling rule:

  • Use tuples such as (priority, data) when the priority value alone determines ordering. section 12.2
  • Use an ordered dataclass when work items need named fields; mark payload fields with compare=False. section 12.2
  • Add a secondary order field when equal priorities must retain a defined sequence. section 12.2

Worker scheduling

Queue (3, 'Lowest priority'), (2, 'Medium priority'), and (1, 'High priority') in reverse order. A worker retrieves them as high, medium, then low priority, while queue.join() coordinates completion. section 12.2

Listing 12.5 Priority queues with tuples
import asyncio
from asyncio import Queue, PriorityQueue
from typing import Tuple
 
 
async def worker(queue: Queue):
    while not queue.empty():
        work_item: Tuple[int, str] = await queue.get()
        print(f'Processing work item {work_item}')
        queue.task_done()
 
 
async def main():
    priority_queue = PriorityQueue()
 
    work_items = [(3, 'Lowest priority'),
                  (2, 'Medium priority'),
                  (1, 'High priority')]
 
    worker_task = asyncio.create_task(worker(priority_queue))
 
    for work in work_items:
        priority_queue.put_nowait(work)
 
    await asyncio.gather(priority_queue.join(), worker_task)
 
 
asyncio.run(main())

Runnable tuple-based asyncio example showing queue creation, worker retrieval, task_done(), and priority-ordered output. section 12.2

How Python Concurrency with asyncio covers it

How deep it goes
Explains heap-backed ordering, tuple and dataclass representations, priority ties, and a web-application use case.
Examples
Demonstrates tuple items, ordered WorkItem dataclasses, tied priorities, explicit tie-breaking, and prioritized web orders.
What it assumes
Assumes asynchronous workers and queues while introducing heap ordering, priority representation, and tie-breaking progressively.
What you can do after
Build it from scratch with prioritized tuples or dataclasses and coordinate workers using queue retrieval and completion tracking.
How it connects
Connects asyncio queues with heaps, dataclasses, workers, task completion, and aiohttp web applications.

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.