Doing something before program exit

Question:

How can you have a function or something that will be executed before your program quits? I have a script that will be constantly running in the background, and I need it to save some data to a file before it exits. Is there a standard way of doing this?

Asked By: RacecaR

||

Answers:

Check out the atexit module:

http://docs.python.org/library/atexit.html

For example, if I wanted to print a message when my application was terminating:

import atexit

def exit_handler():
    print 'My application is ending!'

atexit.register(exit_handler)

Just be aware that this works great for normal termination of the script, but it won’t get called in all cases (e.g. fatal internal errors).

Answered By: Brent Writes Code

If you stop the script by raising a KeyboardInterrupt (e.g. by pressing Ctrl-C), you can catch that just as a standard exception. You can also catch SystemExit in the same way.

try:
    ...
except KeyboardInterrupt:
    # clean up
    raise

I mention this just so that you know about it; the ‘right’ way to do this is the atexit module mentioned above.

Answered By: Katriel

If you want something to always run, even on errors, use try: finally: like this –

def main():
    try:
        execute_app()
    finally:
        handle_cleanup()

if __name__=='__main__':
    main()

If you want to also handle exceptions you can insert an except: before the finally:

Answered By: Brian C. Lane

If you have class objects, that exists during the whole lifetime of the program, you can also execute commands from the classes with the __del__(self) method:

class x:
def __init__(self):
    while True:
        print ("running")
        sleep(1)

def __del__(self):
    print("destructuring")


a = x()

this works on normal program end as well if the execution is aborted, for sure there will be some exceptions:

running
running
running
running
running
Traceback (most recent call last):
  File "x.py", line 14, in <module>
    a = x()
  File "x.py", line 8, in __init__
    sleep(1)
KeyboardInterrupt
destructuring
Answered By: Rastefan

This is a version adapted from other answers.
It should work (not fully tested) with graceful exits, kills, and PyCharm stop button (the last one I can confirm).

import signal
import atexit


def handle_exit(*args):
    try:
        ... do computation ...
    except BaseException as exception:
        ... handle the exception ...


atexit.register(handle_exit)
signal.signal(signal.SIGTERM, handle_exit)
signal.signal(signal.SIGINT, handle_exit)
Answered By: GarouDan

Based on answers here:

import sys
import atexit
import signal

def exit_handler():
    print("Cleaning up")

def kill_handler(*args):
    sys.exit(0)

atexit.register(exit_handler)
signal.signal(signal.SIGINT, kill_handler)
signal.signal(signal.SIGTERM, kill_handler)

# MAIN PROGRAM
# for example just reading from the input:
input("Press enter: ")

Answered By: Alek
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.