graceful-killer
An extremely simple package with a single utillity class used for gracefully handling POSIX shutdown signals.
Installation
Use pip
to install this package straight from GitHub.
pip install git+https://github.com/svencurkovic/graceful-killer-py
Usage
Simply initialize it like so.
from graceful_killer import GracefulKiller
gk = GracefulKiller()
Now you can check the boolean gk.exit
variable to determine whether or not your program should exit.
For example, if I have a program with an infinite loop, I can keep checking the variable on each iteration.
while not gk.exit:
# do stuff...
When a signal (SIGINT, SIGTERM, SIGHUP) is sent to your program, the variable gk.exit
will become True
and the program will exit.
Don't forget to get well acquainted with how signals are distributed in multithreaded and/or multiprocessing environments to make sure you handle exit conditions correctly.