I have a python process which runs in background, and I would like it to generate some output only when the script is terminated.
def handle_exit(): print('\nAll files saved in ' + directory) generate_output() atexit.register(handle_exit) Calling raising a KeyboardInterupt exception and sys.exit() calls handle_exit() properly, but if I were to do kill {PID} from the terminal it terminates the script without calling handle_exit().
Is there a way to terminate the process that is running in the background, and still have it run handle_exit() before terminating?
The functions registered via this module are not called when the program is killed by a signal not handled by Python, when a Python fatal internal error is detected, or when os._exit() is called.here