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 the timeout
parameter is None
, the function will run in the same way, but without time limits.
result = 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
timelimit-0.0.3.tar.gz
(3.6 kB
view hashes)
Built Distribution
Close
Hashes for timelimit-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3561a8591de31411aa0aef8e2aa28d6a046946a209c4a5bfb1bb5a25da7b07b4 |
|
MD5 | 3738a8e0157bb2ac1ddb9bacd522f0b4 |
|
BLAKE2b-256 | 07e381344b8f828b482e04a0ca1d3964eb7ebafae733ba8f71be3dea0eb4a060 |