Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 40 ยท Synchronization and Queues

PriorityQueue, LifoQueue and Queue Design Patterns

Learn asyncio.PriorityQueue and LifoQueue, tie-breaking with counters, and build multi-stage async pipelines and fan-out/fan-in designs with queues.

An asyncio.Queue hands items out in the order they arrived (first in, first out). Two sibling classes change that order while keeping exactly the same API (put, get, task_done, join, maxsize, shutdown). This lesson covers them, and then shows how queues combine into multi-stage pipelines, the most useful design pattern in this chapter.

PriorityQueue: urgent items first

asyncio.PriorityQueue always returns the smallest item first. By convention you enqueue tuples (priority, payload), where a lower number means more urgent:

Python Lowest priority number comes out first

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Items with the same priority are compared by the next element of the tuple, here the text (so "fix production bug" before "restart database" alphabetically). That works for strings, but breaks when the payload cannot be compared.

The tie-breaker problem

If two items have equal priorities and the payloads are not comparable (dictionaries, custom objects, coroutines), Python raises TypeError: '<' not supported. The standard cure is to add a sequence counter between priority and payload. It also gives FIFO order within one priority:

Python A counter as tie-breaker

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Without the counter this would crash as soon as two priorities tie. Within each priority the original arrival order is preserved.

A priority worker

Workers pick the most urgent waiting job. When an urgent job arrives while a worker is busy, it will be next in line, ahead of the older low-priority work:

Python Urgent jobs jump the queue

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The worker was already busy with report A when the urgent job arrived. As soon as it finished, it picked SERVER DOWN before report B. Note that priority queues do not interrupt a running job; they only decide who goes next.

A steady stream of urgent items can starve low-priority work forever. If that matters, add aging (improve the priority of items that wait too long) or run separate workers for low-priority jobs.

LifoQueue: newest first

asyncio.LifoQueue is a stack: the last item put in is the first one out. It is useful when the most recent item matters most, such as depth-first exploration or "undo" histories:

Python LifoQueue is a stack

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Pipelines: queues between stages

The most powerful use of queues is a pipeline: stage 1 reads or downloads, stage 2 transforms, stage 3 stores. Each stage is a group of tasks, and queues connect them. Every stage works concurrently with the others, and bounded queues keep memory in check.

flowchart LR
    S["Source<br/>generate numbers"] -->|"raw queue"| T["Transform<br/>2 workers"]
    T -->|"result queue"| K["Sink<br/>collect results"]

Each arrow is a queue. While the sink stores item 1, the transformers process item 2 and the source makes item 3.

Python A three-stage pipeline

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two transform workers share the raw queue; each stage ends when it sees its sentinel. The sink counts one DONE per worker so it knows when all upstream workers are finished. The bounded queues mean a fast source can never run far ahead of slow workers.

Fan-out and fan-in

The pipeline above uses both classic shapes:

  • Fan-out: one queue feeds many workers that share the load.
  • Fan-in: many workers feed one queue that a single consumer reads.

With the same ideas you can build log aggregators, ETL jobs, download managers, or a chat server that broadcasts each message into one queue per connected user.

Choosing a design

Situation Pattern
Equal jobs, limit concurrency Queue plus N workers
Some jobs are more urgent PriorityQueue with a counter tie-breaker
Most recent first LifoQueue
Multi-step processing one queue between each pair of stages
Slow consumers, fast producers bounded maxsize

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In a PriorityQueue, which item does get() return first?

    The smallest item comes out first, so a lower priority number means more urgent.
  2. 2. Why add itertools.count() between the priority and the payload?

    Tuples compare element by element, so a unique counter ensures the payload is never compared.
  3. 3. A LifoQueue holds 1, 2, 3 (put in that order). What does get() return first?

    LifoQueue is a stack: last in, first out.
  4. 4. In a pipeline, why use bounded queues between stages?

    A full queue makes put() wait, so a fast stage cannot flood a slow one.

Summary

  • PriorityQueue returns the smallest item first; use (priority, counter, payload) tuples.
  • LifoQueue returns the newest item first, like a stack.
  • Priority queues decide who is next but never interrupt running jobs; beware starvation.
  • Queues chain into pipelines with fan-out and fan-in, with sentinels or shutdown() to close each stage.
  • Bounded queues give backpressure through the whole pipeline.

Next: Streams - StreamReader and StreamWriter