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!")
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.1.tar.gz
(3.6 kB
view hashes)
Built Distribution
Close
Hashes for timelimit-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 170f799d7f7fc364174c6fd074d67a20f346dd68e25e6bf1293c2d4d63423eb3 |
|
MD5 | a0f983c5fd73358878891cf1bb88a2aa |
|
BLAKE2b-256 | c69bf030b47349ac8a3219a7b7a90c8e62c1f326bad3220e6c3a27103d542251 |