timeoutd
Installation
From PyPI:
pip install timeoutd
From source code:
git clone https://github.com/juhannc/timeoutd.git
pip install -e .
Usage
The timeoutd module provides a decorator that can be used to limit the execution time of a function.
The decorator takes a single argument, the number of seconds or a specific date (as a datetime object) after which the function should be terminated.
import time
import timeoutd
@timeoutd.timeout(5)
def mytest():
print("Start")
for i in range(1, 10):
time.sleep(1)
print(f"{i} seconds have passed")
if __name__ == '__main__':
mytest()
The timeout decorator allows for multiple different ways to specify the timeout, for example with a datetime object:
import time
import datetime
import timeoutd
@timeoutd.timeout(datetime.datetime.now() + datetime.timedelta(0, 5))
def mytest():
print("Start")
for i in range(1, 10):
time.sleep(1)
print(f"{i} seconds have passed")
if __name__ == '__main__':
mytest()
It also take a timedelta object:
import time
import datetime
import timeoutd
@timeoutd.timeout(datetime.timedelta(0, 5))
def mytest():
print("Start")
for i in range(1, 10):
time.sleep(1)
print(f"{i} seconds have passed")
if __name__ == '__main__':
mytest()
But it can also take a delta in form of hours, minutes, and seconds via the kwargs:
import time
import timeoutd
@timeoutd.timeout(hours=0, minutes=0, seconds=5)
def mytest():
print("Start")
for i in range(1, 10):
time.sleep(1)
print(f"{i} seconds have passed")
if __name__ == '__main__':
mytest()
The timeout decorator also accepts a custom exception to raise on timeout:
import time
import timeoutd
@timeoutd.timeout(5, exception_type=StopIteration)
def mytest():
print("Start")
for i in range(1, 10):
time.sleep(1)
print(f"{i} seconds have passed")
if __name__ == '__main__':
mytest()
You can also specify a function to be called on timeout instead of raising an exception:
import time
import timeoutd
def add_two_numbers(i: int, j: int | None = None):
if j is None:
j = 0
print(f"The sum of {i = } and {j = } is {i + j}")
@timeoutd.timeout(
5,
on_timeout=add_two_numbers,
on_timeout_args=(1,),
on_timeout_kwargs={"j": 2}
)
def mytest():
print("Start")
for i in range(1, 10):
time.sleep(1)
print(f"{i} seconds have passed")
if __name__ == '__main__':
mytest()
Multithreading
Note: This feature appears to be broken in some cases for the original timeout-decorator. Some issues might still exist in this fork.
By default, timeoutd uses signals to limit the execution time of the given function.
This approach does not work if your function is executed not in a main thread (for example if it's a worker thread of the web application).
There is alternative timeout strategy for this case - by using multiprocessing.
To use it, just pass use_signals=False to the timeout decorator function:
import time
import timeoutd
@timeoutd.timeout(5, use_signals=False)
def mytest():
print "Start"
for i in range(1, 10):
time.sleep(1)
print("{} seconds have passed".format(i))
if __name__ == '__main__':
mytest()
Warning: Make sure that in case of multiprocessing strategy for timeout, your function does not return objects which cannot be pickled, otherwise it will fail at marshalling it between master and child processes.
Acknowledgement
Derived from http://www.saltycrane.com/blog/2010/04/using-python-timeout-decorator-uploading-s3/, https://code.google.com/p/verse-quiz/source/browse/trunk/timeout.py, and https://github.com/pnpnpn/timeout-decorator
Metadata
Release files for timeoutd 0.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| timeoutd-0.7.0.tar.gz | 9.7 kB | Without provenance | Details for timeoutd-0.7.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| timeoutd-0.7.0-py3-none-any.whl | Python 3 | none | any | Details for timeoutd-0.7.0-py3-none-any.whl |
Total release size: 19.4 kB
Release files / timeoutd-0.7.0.tar.gz
| Download URL | timeoutd-0.7.0.tar.gz |
|---|---|
| Size | 9.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
583b271307cdcea9ebb1203da64e0213166d52364bb13958721ec7a08877ed19
|
|
BLAKE2b-256 checksum How to use checksums |
19132c459fc9a396dab6804b4d85186be9725e568ff7391d6d9735f68e418ed2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.16
|
Release files / timeoutd-0.7.0-py3-none-any.whl
| Download URL | timeoutd-0.7.0-py3-none-any.whl |
|---|---|
| Size | 9.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
957a47287f89f609d8a505afe8b4914f8d4a1e50be9016063e3aedee2e425244
|
|
BLAKE2b-256 checksum How to use checksums |
bdde694cc4145d8eff62a14accd402a4a2ff538cda10ed03cda35074a4e3d8de
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.16
|
Security for timeoutd 0.7.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.