Skip to main content

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

timelimit-0.1.0.tar.gz (3.9 kB view hashes)

Uploaded Source

Built Distribution

timelimit-0.1.0-py3-none-any.whl (4.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page