Python output handlers

I am a little confused about the many different ways of setting exit handlers in Python applications. There atexit.register()

and signal.signal(SIG, handler)

, m unsure, which one has the right to use in my case.

I have a main process running from the command line that spawns a number of other sub-processes as daemons. Then it join

processes and waits for them to finish. Subprocesses loop endlessly (may break out with a flag, but not sure how to run it). I would like to name some cleanup code in subprocesses where the main process either exits with CTRL + C or when it receives a kill signal.

What's the best way to achieve this, given 2 output handler methods (or maybe more).

+3


source to share


1 answer


From the documentation atexit

:

Registered functions are automatically executed when the normal interpreter exits.

and



Note. Functions registered through this module are not called when the program is killed by a signal that is not handled by Python, when a fatal internal Python error is encountered, or when os._exit () is called.

So, if you want to react to signals, use signal.signal

.

+1


source







All Articles