Run atexit() when python process is killed
Asked Answered
P

4

25

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?

Prosimian answered 29/11, 2016 at 13:0 Comment(1)
Not possible with atexit alone. As the documentation states 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. hereGuncotton
S
37

Try signal.signal. It allows to catch any system signal:

import signal

def handle_exit():
    print('\nAll files saved in ' + directory)
    generate_output()

atexit.register(handle_exit)
signal.signal(signal.SIGTERM, handle_exit)
signal.signal(signal.SIGINT, handle_exit)

Now you can kill {pid} and handle_exit will be executed.

Shaylyn answered 29/11, 2016 at 13:19 Comment(3)
N.B., your handler function signatures should afford two positional arguments – a signal number and a stack frame, e.g. handle_exit(signum, frame) or handle_exit(*args) – as that’s how those functions invoked via signal.signal(…) will be called. If your functions take explicit arguments, you can set up values for atexit.register(…) to pass on its own invocation, e.g. atexit.register(handle_exit, None, None) or similar.Portmanteau
Doesn't work on Windows when closing via Task Manager or via Stop button in PyCharmJacy
This will execute handle_exit twice - once from handle_exit and then again from atexit.register.Himself
S
1

To enable signals when debugging PyCharm on Windows:

  1. Within PyCharm hit Ctrl + Shift + A to bring up the "Find Actions..." menu
  2. Search for "Registry" and hit enter
  3. Find the key kill.windows.processes.softly and enable it (you can start typing "kill" and it will search for the key)
  4. Restart PyCharm
Simferopol answered 11/1, 2021 at 17:1 Comment(0)
B
1

If you want to handle signals yourself, according to the Python documentation, the handler function needs two arguments.

If you handle the SIGINT signal, you must call sys.exit(0) within the handler function; otherwise, the program will not terminate upon pressing Ctrl + C.

As KamiCuk mentioned, if the function registered with atexit is the same as the signal handler function, the function will be called twice.

Therefore, I prefer using separate functions for the signal handler and for the atexit registration. Here is an example:

import atexit
import signal
import sys

def on_exit():
    print('real clean code here')

def handle_exit(signum, frame):
    sys.exit(0)

atexit.register(on_exit)
signal.signal(signal.SIGTERM, handle_exit)
signal.signal(signal.SIGINT, handle_exit)

Handling these signals correctly can be tricky, especially for cross-platform applications.

To address this, I have created a package called safe-exit.

The documentation can be found here: https://safe-exit.readthedocs.io/en/latest/

Bertero answered 15/5, 2023 at 13:3 Comment(0)
S
0

To check your system and see which signal is being called:

import signal
import time


def handle_signal(sig_id, frame):
    sig = {x.value: x for x in signal.valid_signals()}.get(sig_id)
    print(f'{sig.name}, {sig_id=}, {frame=}')
    exit(-1)


for sig in signal.valid_signals():
    print(f'{sig.value}: signal.{sig.name},')
    signal.signal(sig, handle_signal)

time.sleep(30)
Simferopol answered 11/1, 2021 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.