Python 3.8+signalfunction
signal.raise_signal()
sending signals
Sends a signal to the current process. Handy for testing handlers; it works on Windows too.
Syntax
signal.raise_signal(signum)
Unlike os.kill(os.getpid(), sig), raise_signal also works on Windows and targets the calling thread, so the Python handler runs right after the call, before the next line. Without a handler the OS default applies: raise_signal(SIGTERM) ends the process with no finally. A handler that raises makes the exception come out of the raise_signal line.
Examples
import signal
def handler(signum, frame):
raise RuntimeError(signal.Signals(signum).name)
old = signal.signal(signal.SIGTERM, handler)
try:
signal.raise_signal(signal.SIGTERM)
except RuntimeError as e:
print(e) # → SIGTERM — исключение вылетело из raise_signal
signal.signal(signal.SIGTERM, old)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy