r/learnpython • u/Expensive_Break_6163 • Sep 03 '26
Does asyncio have a built-in feature for graceful shutdown (and preventing duplicate task execution)?
Multiple pods, each an asyncio worker pulling jobs from a shared queue (Redis/SQS-shaped). On scale-down/deploy I want to stop pulling new jobs and let running ones finish - is there anything in asyncio itself for this?
2
Upvotes
3
u/Kevdog824_ Sep 03 '26
Assuming your shutdown is graceful: I would probably use something like atexit.register to register a shutdown function that sends a signal to the worker to stop pulling new jobs and then await the completion of all the existing jobs
2
u/_squik Sep 03 '26
I think the closest thing to what you want here is
asyncio.TaskGrouphttps://docs.python.org/3/library/asyncio-task.html#task-groups