Code

The Parcel Queue

An asyncio.Queue connects coroutines like a conveyor belt. Producers put items on it and consumers take them off:

queue = asyncio.Queue(maxsize=2)
await queue.put("oil")    # waits if the queue is full
item = await queue.get()  # waits if the queue is empty

maxsize gives you backpressure. A fast producer has to wait for a slow consumer instead of piling up a mountain of items in memory.

How does the consumer know when to stop? A common trick is a sentinel, a special "no more items" value such as None:

while True:
    item = await queue.get()
    if item is None:
        break
    handle(item)

(For long-running workers there's also queue.task_done() and await queue.join(), which waits until every item has been processed.)

In the starter, main() wraps the work in asyncio.timeout(2) as a safety net. If you forget the sentinel, the courier would wait forever. The timeout turns that freeze into a clear TimeoutError.

Assignment

  1. producer(queue, parcels): put each parcel on the queue, then put None.
  2. courier(queue, delivered): keep taking items off the queue. Stop when you get None. For every other item, append "delivered <item>" to the delivered list.