I'm a bit confused by the many different ways to install exit handlers in Python applications. There's atexit.register()
and signal.signal(SIG, handler)
, but I'm unsure which one is the right to use in my case.
I've got a main process started from the command line which spawns a number of other sub processes as daemons. It then join
s the processes and waits until they finish. The sub processes run an infinite loop (can break out by a flag, but not sure how to trigger that). I'd like to call some cleanup code in the sub processes when the main process gets either shut down via CTRL+C or when it receives a kill signal.
What's the best way of achieving this, given the 2 exit handler methods (or perhaps there're more).
From the documentation of
atexit
:and
So if you want to react to signals, use
signal.signal
.