Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 60f04f0

Browse files
committed
new module for interruptable threads
1 parent bd669dd commit 60f04f0

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

lib/utils/timeout.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
3+
import threading
4+
5+
def timeout(func, args=(), kwargs={}, timeout_duration=1, default=None):
6+
class InterruptableThread(threading.Thread):
7+
def __init__(self):
8+
threading.Thread.__init__(self)
9+
self.result = None
10+
11+
def run(self):
12+
try:
13+
self.result = func(*args, **kwargs)
14+
except:
15+
self.result = default
16+
17+
thread = InterruptableThread()
18+
thread.start()
19+
thread.join(timeout_duration)
20+
if thread.isAlive():
21+
return default
22+
else:
23+
return thread.result

0 commit comments

Comments
 (0)