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?
AdvancedAnswer
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 cleanlyReal-world example Building a production task-processing service that shuts down gracefully on SIGTERM, finishing in-flight work rather than dropping it.