CodeRigor

FREE LESSON

Bounded FIFO & priority queues

Separate admission, service order and stable priority ties in a bounded in-memory buffer.

A background job queue holds work that a producer has accepted but a consumer has not yet removed. A queue buys time when arrival and processing rates differ; it cannot make sustained overload disappear. If producers add ten jobs per tick and consumers remove six, the backlog grows by four each tick until admission changes. Capacity makes that decision visible before memory grows without a bound.

Start with two independent questions: may this job enter? and which accepted job leaves next? The build rejects a push when the waiting count equals capacity. Rejection returns False and preserves the queue; the producer still owns the rejected job and must choose whether to retry later, persist elsewhere or report failure. Silently discarding an old entry would be a different contract. Repeatedly retrying immediately would spend CPU without increasing capacity.

A bounded admission boundary
start / 3
Rejecting a full buffer leaves ownership with the producer.
A bounded admission boundary
Rejecting a full buffer leaves ownership with the producer.

FIFO means remove the oldest accepted waiting job. In priority mode the smallest numeric priority wins. That convention is ours; another system may assign greater urgency to larger numbers. Arrival order breaks equal-priority ties. Include a sequence number in heap entries so equal priorities never compare arbitrary payloads. Two dictionaries cannot be ordered with <, and ordering by a job label would reorder ties for an unrelated reason.

Python
from collections import deque
import heapq

ready = deque(["first", "second"])
print(ready.popleft())
heap = []
for sequence, (priority, job) in enumerate([(4, "slow"), (1, "urgent"), (1, "also urgent")]):
    heapq.heappush(heap, (priority, sequence, job))
print([heapq.heappop(heap)[2] for _ in range(3)])

Inspection must not consume work. items() returns a fresh service-order list; sorting a heap copy can provide that view. A heap's internal list is only partially ordered, so copying that internal list alone does not promise service order. The returned list shares payload references by contract; replacing its entries changes the view, while mutating a referenced payload still affects that payload.

Priority ties keep arrival order
start / 2
Priority 1 precedes 4; equal priorities retain their arrival sequence.
Priority ties keep arrival order
Priority 1 precedes 4; equal priorities retain their arrival sequence.

Empty is an expected state, represented by IndexError for pop; it is different from a stored label or a false admission result. Test capacity one, exact fullness, removal followed by admission, equal priorities and payloads that cannot be compared. In FIFO mode a supplied priority still receives validation, then has no ordering effect. This keeps invalid input distinguishable from a legitimately full buffer.

The exercises implement an ordinary Python object called in a known sequence. They establish admission and ordering behavior for that object. They do not establish thread synchronization, process persistence, broker routing or a real-time scheduling guarantee. Python's queue.Queue offers synchronized in-process operations; the distributed-application lessons that follow add ownership and delivery policies instead of lock-free or RTOS semantics.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →

Watch 2:00–8:30 for publisher, queue and consumer roles. The broker setup and JavaScript code are optional context; the Python buffer and its precise bound are taught here.