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
