-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
66 lines (51 loc) · 1.38 KB
/
utils.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import signal
from contextlib import contextmanager
###########################################################
# Signal Handling
###########################################################
class Signal:
SIGINT = signal.SIGINT
SIGTERM = signal.SIGTERM
def __init__(self):
self.__caught = dict()
def install(self, siglist):
for signum in siglist:
self.__caught[signum] = False
signal.signal(signum, self.__handler)
def __handler(self, signum, frame):
self.__caught[signum] = True
def caught(self, signum=None):
if signum:
if signum in self.__caught:
return self.__caught[signum]
else:
return False
for signum in self.__caught.keys():
if self.__caught[signum]:
return True
return False
class TimeoutException(Exception):
pass
@contextmanager
def time_limit(seconds):
def signal_handler(signum, frame):
raise TimeoutException
signal.signal(signal.SIGALRM, signal_handler)
signal.alarm(seconds)
try:
yield
finally:
signal.alarm(0)
###########################################################
# Interruptible loop
###########################################################
def for_each_item_int(item_list):
# register signal handler
signal = Signal()
signal.install([Signal.SIGINT, Signal.SIGTERM])
# work with each item
for idx, item in enumerate(item_list):
# check for interruption
if signal.caught():
break
yield idx, item