root/trunk/lib/timeouts.py @ 14703
1 |
# Timeouts
|
---|---|
2 |
|
3 |
import signal |
4 |
|
5 |
class TimeoutException(Exception): pass |
6 |
|
7 |
def run(func, timeout): |
8 |
'''@param timeout sec'''
|
9 |
def handle_timeout(*args): raise TimeoutException |
10 |
prev_handler = signal.signal(signal.SIGALRM, handle_timeout) |
11 |
signal.alarm(timeout) |
12 |
try: return func() |
13 |
finally: |
14 |
signal.alarm(0) |
15 |
signal.signal(signal.SIGALRM, prev_handler) |