all Python 3signalterm
signal
module
Handlers for asynchronous OS signals: catching Ctrl+C and SIGTERM, alarm timers, signal masks.
Syntax
import signal
signal.signal installs a function the interpreter calls when a signal arrives — the standard way to shut a service down cleanly on SIGTERM. Handlers run only in the main thread and between bytecode instructions, so they cannot interrupt a long C call. The set of signals depends on the OS: Windows has only a few, and alarm, setitimer and signal masks are Unix-only.
Examples
import signal
def handler(signum, frame):
print('got', signal.Signals(signum).name) # → got SIGTERM
signal.signal(signal.SIGTERM, handler)
signal.raise_signal(signal.SIGTERM)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy