diff options
author | PN <pn.appdev@gmail.com> | 2012-12-13 13:48:58 -0500 |
---|---|---|
committer | PN <pn.appdev@gmail.com> | 2012-12-13 13:48:58 -0500 |
commit | cae118e6a52ef508d6e6e8f9dbfb5959896ad39a (patch) | |
tree | f4a33718c5fd58e5dcc129041b32168981490a88 | |
parent | 577b4ec85fdd4a41e871bc4a63e72f519e429361 (diff) | |
download | timeout-decorator-cae118e6a52ef508d6e6e8f9dbfb5959896ad39a.tar.gz |
init
-rw-r--r-- | .gitignore | 38 | ||||
-rw-r--r-- | LICENSE | 22 | ||||
-rw-r--r-- | MANIFEST.in | 1 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | README.rst | 11 | ||||
-rw-r--r-- | timeout_decorator.py | 35 |
6 files changed, 107 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bd77452 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +*.py[cod] +*.swp +*~ +venv + +# C extensions +*.so + +# Packages +*.egg +*.egg-info +dist +build +eggs +parts +bin +var +sdist +develop-eggs +.installed.cfg +lib +lib64 + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox +nosetests.xml + +# Translations +*.mo + +# Mr Developer +.mr.developer.cfg +.project +.pydevproject @@ -0,0 +1,22 @@ +Copyright (c) 2012 PN + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..ff7d844 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include README.rst LICENSE tests/*.py diff --git a/README.md b/README.md deleted file mode 100644 index 7d8dfb8..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -timeout-decorator -=================
\ No newline at end of file diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..f44c2cb --- /dev/null +++ b/README.rst @@ -0,0 +1,11 @@ + + + +Installation +------------ + + +Usage +----- +:: + diff --git a/timeout_decorator.py b/timeout_decorator.py new file mode 100644 index 0000000..498d01c --- /dev/null +++ b/timeout_decorator.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python + +from __future__ import print_function +from __future__ import unicode_literals +from __future__ import division + +import signal + + +############################################################ +# Timeout +############################################################ + +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 |