diff options
| author | jeanpouget-abadie <jean.pougetabadie@gmail.com> | 2014-12-07 12:08:31 -0500 |
|---|---|---|
| committer | jeanpouget-abadie <jean.pougetabadie@gmail.com> | 2014-12-07 12:08:31 -0500 |
| commit | 9de35421f25bf45158187daea4ddfedd1c93f3d8 (patch) | |
| tree | f917008b6363a2b9dbff7855781f4fd5a10a6e94 /src/timeout.py | |
| parent | 6c874852773329f6fecbbc54476b30a37aa85b79 (diff) | |
| download | cascades-9de35421f25bf45158187daea4ddfedd1c93f3d8.tar.gz | |
renaming directory + creating dataset directory
Diffstat (limited to 'src/timeout.py')
| -rw-r--r-- | src/timeout.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/timeout.py b/src/timeout.py new file mode 100644 index 0000000..d7381c3 --- /dev/null +++ b/src/timeout.py @@ -0,0 +1,25 @@ +from functools import wraps +import errno +import os +import signal + +class TimeoutError(Exception): + pass + +def timeout(seconds=10, error_message=os.strerror(errno.ETIME)): + def decorator(func): + def _handle_timeout(signum, frame): + raise TimeoutError(error_message) + + def wrapper(*args, **kwargs): + signal.signal(signal.SIGALRM, _handle_timeout) + signal.alarm(seconds) + try: + result = func(*args, **kwargs) + finally: + signal.alarm(0) + return result + + return wraps(func)(wrapper) + + return decorator |
