py-timeout - pure Python process based decorator that provides execution timeout for Python functions and methods. Since it is not based on signals, py-timeout works fine outside the main thread.
py-timeout requires python 3.6 or newer versions to run.
Cloning project from git repository
git clone https://github.com/YADRO-KNS/py-timeout.git
Installing from PyPi
pip3 install py-timeout
import timeout
@timeout.timeout(duration=0.5)
def foo(value: int) -> None:
...
...
@timeout.timeout(duration=0.5)
def bar(self, value: str) -> str:
...
Decorated function or method will be executed as a sub-process with expected life-time of passed duration value.
import timeout
import time
@timeout.timeout(duration=5)
def foo() -> None:
while True:
time.sleep(1)
try:
foo()
except timeout.TimeoutException:
pass
In case if for some reason execution will take longer than expected the process will be terminated and TimeoutException will be raised.
We use SemVer for versioning.
See also the list of contributors who participated in this project.
The code is available as open source under the terms of the MIT License.