Sets the time limit for slow-running functions.
Project description
timelimit
Sets the time limit for slow-running functions. Runs functions in parallel threads or processes.
Tested with Python 3.6-3.9 on macOS, Ubuntu and Windows.
Install
pip3 install timelimit
Use
from timelimit import limit_thread, limit_process, TimeLimitExceeded
def sluggish(a, b):
...
return a + b
# will run sluggish(1, 2) in parallel thread no more than 5 seconds
a_plus_b = limit_thread(sluggish, (1, 2), timeout=5)
# will run sluggish(1, 2) in parallel process no more than 5 seconds
a_plus_b = limit_process(sluggish, (1, 2), timeout=5)
If the time is up
If the function did not complete its work within the specified time, a
TimeLimitExceeded exception is thrown.
try:
limit_thread(sluggish, (1, 2), timeout=5)
except TimeLimitExceeded:
print("Oops!")
If you set the default argument (at least to None), the default value is
returned instead of an exception.
result = limit_thread(sluggish, (1, 2), timeout=5, default=-1)
if result == -1:
print("Oops!")
If time doesn't matter
If you do not specify the timeout parameter it will default to float('inf').
The sluggish function will run in a parallel thread or process, but without
time constraints.
# both call run the function in parallel thread without time limits
limit_thread(sluggish, (1, 2))
limit_thread(sluggish, (1, 2), timeout=float('inf'))
If you specify the value timeout = None, then the sluggish will be
executed like a regular function, without starting processes or threads.
# the following calls are equivalent
sluggish(1, 2)
limit_thread(sluggish, (1, 2), timeout=None)
Thus, the limitation can be made optional and resource-saving.
limit_thread(sluggish, (1, 2),
timeout = 5 if in_hurry else None)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file timelimit-0.1.0.tar.gz.
File metadata
- Download URL: timelimit-0.1.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7584d7b3bfb7b6fd7e5c4323617ab4d6e7859ac355851ee220d5f9c2d2d9b10f
|
|
| MD5 |
99e1cab6dc4b319b0cbe9342dc9bfee7
|
|
| BLAKE2b-256 |
ae680d15258ce5a4c1017f5ff8a31f4cf8eb8abc017844f771c1a28ff8795792
|
File details
Details for the file timelimit-0.1.0-py3-none-any.whl.
File metadata
- Download URL: timelimit-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
efd85e7e67afdfe78fc3d8ef3af0f032a2b666174f674009813058c6846d9589
|
|
| MD5 |
b8f851b3e17fb7a00399cb3ed0c229ce
|
|
| BLAKE2b-256 |
a1b4dbf41a909bac625866a8c03c1818eed43076ea97178a05ee0272efee0507
|