-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeout.py
More file actions
57 lines (47 loc) · 1.19 KB
/
Timeout.py
File metadata and controls
57 lines (47 loc) · 1.19 KB
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
import signal
class TimeoutError(Exception):
"""
Custom Error for Timeout
"""
pass
class Timeout(object):
"""
Timeout wrapper for signal.alarm
Currently only compatible with linux systems, need to update for windows
"""
def __init__(self, sec):
self.sec = sec
def __enter__(self):
"""
Enter method to allow use of with
Parameters
----------
Returns
---------
"""
signal.signal(signal.SIGALRM, self.raise_timeout)
signal.alarm(self.sec)
def __exit__(self, type, value, traceback):
"""
Closes event_loop on exit from with
Parameters
----------
Returns
---------
"""
# Reset alarm
signal.alarm(0)
if type is not None:
raise
def raise_timeout(self, *args):
"""
Closes event_loop on exit from with
Parameters
----------
*args : 'signal.signal handler args'
signal number and frame
Returns
---------
"""
raise TimeoutError("Connection timed out after {:} seconds!"
.format(self.sec))