PythiloLesson 8 of 12 · progress kept in this browser

Drain a bounded queue before shutdown

A queue is not only a container: join tracks unfinished work, and each successful get requires one task_done call. Graceful shutdown stops new production while consumers drain accepted items. Immediate shutdown has a different contract and can abandon queued work.

Python 3.14 provides Queue.shutdown. The local exercise uses its non-immediate mode and verifies that every accepted item finishes before returning. Returning from a producer without joining consumers can report success while writes are still pending. No worker thread or external service is needed to observe this lifecycle.

import asyncio
async def main():
    q=asyncio.Queue()
    await q.put(4)
    q.shutdown()
    print(await q.get())
    q.task_done()
    await q.join()
asyncio.run(main())
Your task

Implement async drain(values) with a bounded asyncio.Queue(maxsize=2), one consumer and graceful shutdown. Return processed values doubled in order; call task_done in finally for every received item.