Did you know Python can automatically do things for you when the process exits? The atexit is one of those handy, but often forgotten about, aspects of Python. I had cause to use it yet again today with a certain process that was being naughty.
The idea is simple, atexit instructs Python to run your function just before the process exits. So when would you want to do that? I'm sure there are other situations, but I typically end up using them when:
- I want to log both process startup AND exit
- Alert someone that a process has died, especially cron jobs that are easily forgotten about
A quick example to show you the idea:
import atexit @atexit.register def closing(): print("=== Closing down ===") while True: print("whee!")
Using the atexit decorator, we wrap a little function that just prints out that we're closing down. Our while loop, er I mean our whee loop, will run until we interrupt the process with a Ctrl-C. The output looks like this:
whee! whee! whee! whee! whee! whee! whee! whee! whee! whee! whee! whee! whee^Cwhee! whee! Traceback (most recent call last): File "example.py", line 10, in <module> print("whee!") KeyboardInterrupt === Closing down ===
We see our usual KeyboardInterrupt traceback and then our custom closing message.
You can also register your atexit functions without the decorator so you can pass args and kwargs to it to be flexible.
Seems pretty perfect, doesn't it? It's not all roses. Python only calls these atexit functions in a couple of scenarios, specifically if:
- sys.exit() is called
- The process completes and is exiting normally
However, it does not call your atexit handles if Python is killed by a signal, when Python itself has a fatal internal error, or if os._exit() is called directly. So it's not useful to detect kills or kill -9s for sure, but in most other usual exit scenarios it can be handy.
Hope you find a use for it!