I have a daemon process witch spawns child processes using multiprocessing to do some work, each child process opens its own connection handle do DB (postgres in my case). Jobs to processes are passed via Queue and if queue is empty processes invoke sleep for some time, and recheck queue
How can I implement “graceful shutdown” on SIGTERM? Each subprocess should terminate as fast as possible, with respect of closing/terminating current cursor/transaction and db connection, and opened files.
Store all the open files/connections/etc. in a global structure, and close them all and exit in your SIGTERM handler.