Skip to main content

Timeout wrapper utilities

Project description

Python Timeout Library

This is the source for the timeoutlib python library.

The library solves the following problem: given a computational budget in seconds, and a function that performs some work, ensure that the computation does not take longer than the budgeted duration

In the event that a function times out, you can choose to return a default value, raise an exception or return a default from a default_factory.

Usage

You can use a decorator or functional syntax to wrap your function

import time

import timeoutlib


# Decorator syntax

# will raise timeoutlib.OperationTimedOut if 5s exceeded
@timeoutlib.timeout(max_duration=5)
def worker(simulated_duration):
    time.sleep(simulated_duration)


# will return False if 5s exceeded
@timeoutlib.timeout(max_duration=5, default=False)
def worker_default_value(simulated_duration):
    time.sleep(simulated_duration)
    return True


# will call list() to return a new empty list as a default value if 5s exceeded 
@timeoutlib.timeout(max_duration=5, default_factory=list)
def worker_default_value(simulated_duration):
    time.sleep(simulated_duration)
    return True


# will raise a RuntimeError if 5s exceeded 
@timeoutlib.timeout(max_duration=5, exception=RuntimeError)
def worker_default_value(simulated_duration):
    time.sleep(simulated_duration)
    return True


# The two alternative syntax below are equal


@timeoutlib.timeout(max_duration=2)
def worker_decorator(simulated_duration):
    time.sleep(simulated_duration)
    return True


# Given some function that you want to decorate dynamically
def worker_functional(simulated_duration):
    time.sleep(simulated_duration)
    return True


worker_functional = timeoutlib.timeout(max_duration=2)(worker_functional)

# worker_functional and worker_decorated behave the same

Caveats / Notes

  • Only supports integer durations (no fractional seconds)
  • Uses SIGALRM under the hood (unix only - will not work on Windows)
  • timeoutlib.OperationTimedOut subclasses the builtin TimeoutError
  • The default_factory assumes a function with zero args that returns a new default value
  • Accuracy of the timeout duration depends on the underlying OS, Hardware, and some overhead from the decorator itself.

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

timeoutlib-0.1.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

timeoutlib-0.1.0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file timeoutlib-0.1.0.tar.gz.

File metadata

  • Download URL: timeoutlib-0.1.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.8.5 Linux/4.19.0-20-amd64

File hashes

Hashes for timeoutlib-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cac2c1edc2e6420cdbcc77693ccdf4e2b2630443dd0e5f760c3c36e84200da20
MD5 ab354da1f969d77c917c998cfda9603b
BLAKE2b-256 c2d816fc6530fecdd05b8d517b75356fd0d893364073e84361790c70dee7fcab

See more details on using hashes here.

File details

Details for the file timeoutlib-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: timeoutlib-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.8.5 Linux/4.19.0-20-amd64

File hashes

Hashes for timeoutlib-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 99f79cf2875c2224bde33d57b7ea912fc3a1e3322b81356d6d0bd38e30440547
MD5 e7470e6fb7e6b735f22a4e198157599a
BLAKE2b-256 53a2a0aa9f5da3665ba8167ee336510b86a78f5714c8b358dbefffbfcab5b0b3

See more details on using hashes here.

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