Python › Concurrency (asyncio/threading/multiprocessing)

How would you design a robust worker pool that handles both graceful shutdown (finishing in-flight work) and immediate cancellation (dropping everything), using asyncio?

Advanced

Answer

Use an asyncio.Event or a shutdown flag checked cooperatively by workers for GRACEFUL shutdown (letting in-flight tasks complete before stopping), versus explicitly calling task.cancel() on all worker tasks for IMMEDIATE cancellation — combining both gives you a controllable shutdown sequence, often triggered by an OS signal handler.
async def worker(queue, shutdown_event):
    while not shutdown_event.is_set():
        try:
            item = await asyncio.wait_for(queue.get(), timeout=0.5)
            await process(item)
        except asyncio.TimeoutError:
            continue  # check shutdown_event again

async def graceful_shutdown(shutdown_event):
    shutdown_event.set()  # workers finish current item, then stop cleanly
Real-world example Building a production task-processing service that shuts down gracefully on SIGTERM, finishing in-flight work rather than dropping it.

Common follow-ups: How would you register an OS signal handler (like SIGTERM) to trigger this graceful shutdown sequence in an asyncio application?