r/learnpython • u/Expensive_Break_6163 • 21h ago
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
2
u/_squik 20h ago
I think the closest thing to what you want here is asyncio.TaskGroup
https://docs.python.org/3/library/asyncio-task.html#task-groups
2
u/itlogicpartnersllc 19h ago
asyncio can help with cancellation and taskgroup but graceful shutdown across multiple pods is really a queue/worker coordination problem so you will want to stop consuming first and let in fight jobs finish.
3
u/Kevdog824_ 20h ago
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