File tree Expand file tree Collapse file tree 1 file changed +20
-8
lines changed Expand file tree Collapse file tree 1 file changed +20
-8
lines changed Original file line number Diff line number Diff line change 44'''
55
66import signal
7+ import threading
78from contextlib import contextmanager
9+ from six .moves import _thread
810
911
1012class TimeoutException (Exception ):
@@ -13,11 +15,21 @@ class TimeoutException(Exception):
1315
1416@contextmanager
1517def time_limit (seconds ):
16- def signal_handler (signum , frame ):
17- raise TimeoutException ("Timeout after {} seconds." .format (seconds ))
18- signal .signal (signal .SIGALRM , signal_handler )
19- signal .alarm (seconds )
20- try :
21- yield
22- finally :
23- signal .alarm (0 )
18+ if hasattr (signal , "SIGALRM" ):
19+ def signal_handler (signum , frame ):
20+ raise TimeoutException ("Timeout after {} seconds." .format (seconds ))
21+ signal .signal (signal .SIGALRM , signal_handler )
22+ signal .alarm (seconds )
23+ try :
24+ yield
25+ finally :
26+ signal .alarm (0 )
27+ else :
28+ timer = threading .Timer (seconds , lambda : _thread .interrupt_main ())
29+ timer .start ()
30+ try :
31+ yield
32+ except KeyboardInterrupt :
33+ raise TimeoutException ("Timeout after {} seconds." .format (seconds ))
34+ finally :
35+ timer .cancel ()
You can’t perform that action at this time.
0 commit comments