diff options
author | png <pn.appdev@gmail.com> | 2013-01-04 11:44:31 -0500 |
---|---|---|
committer | png <pn.appdev@gmail.com> | 2013-01-04 11:44:31 -0500 |
commit | 6f816d332ed65a6b0cf48cc3541bee784ad7f24c (patch) | |
tree | 9948b7a00bd6c74e23586e293710a1178aaf0ad8 | |
parent | 7817e05957d9a7b092136c9707b2c6972d533b77 (diff) | |
download | timeout-decorator-6f816d332ed65a6b0cf48cc3541bee784ad7f24c.tar.gz |
added py
-rw-r--r-- | timeout_decorator/timeout_decorator.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/timeout_decorator/timeout_decorator.py b/timeout_decorator/timeout_decorator.py new file mode 100644 index 0000000..9876ad6 --- /dev/null +++ b/timeout_decorator/timeout_decorator.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" + :copyright: (c) 2012-2013 by PN. + :license: MIT, see LICENSE for more details. +""" + +from __future__ import print_function +from __future__ import unicode_literals +from __future__ import division + +import signal + +############################################################ +# Timeout +############################################################ + +#http://www.saltycrane.com/blog/2010/04/using-python-timeout-decorator-uploading-s3/ + +class TimeoutError(Exception): + def __init__(self, value = "Timed Out"): + self.value = value + def __str__(self): + return repr(self.value) + +def timeout(seconds_before_timeout): + def decorate(f): + def handler(signum, frame): + raise TimeoutError() + def new_f(*args, **kwargs): + old = signal.signal(signal.SIGALRM, handler) + signal.alarm(seconds_before_timeout) + try: + result = f(*args, **kwargs) + finally: + signal.signal(signal.SIGALRM, old) + signal.alarm(0) + return result + new_f.func_name = f.func_name + return new_f + return decorate |