/
signal.py
42 lines (34 loc) · 1.1 KB
/
signal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import contextlib
import signal
import sys
intercepted_signals = [
signal.SIGINT,
signal.SIGTERM,
signal.SIGABRT,
] # , signal.SIGQUIT]
@contextlib.contextmanager
def catch_signals(cleanup):
"""
Context manager to catch signals.
During the `with` statement a signal handler is installed to catch SIGINT,
SIGTERM and SIGABRT. On exit from the statement the default handlers from
the operating system are reinstalled. The handler first calles the given
`cleanup` function and then either raises KeyboardInterrupt (on SIGINT) or
calls `sys.exit` (on SIGTERM/SIGABRT).
Raises:
KeyboardInterrupt: received SIGINT
SystemExit: received SIGTERM or SIGABRT
"""
def handler(sig, frame):
cleanup(sig)
if sig == signal.SIGINT:
raise KeyboardInterrupt()
elif sig in (signal.SIGTERM, signal.SIGABRT):
sys.exit(128 & sig)
for sig in intercepted_signals:
signal.signal(sig, handler)
try:
yield
finally:
for sig in intercepted_signals:
signal.signal(sig, signal.SIG_DFL)