Skip to main content

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.

Metadata

Release files for timeoutlib 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for timeoutlib 0.1.0
File Size Uploaded
timeoutlib-0.1.0.tar.gz 3.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for timeoutlib 0.1.0
File Interpreter ABI Platform
timeoutlib-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.3 kB

Release files / timeoutlib-0.1.0.tar.gz

Download URL timeoutlib-0.1.0.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
cac2c1edc2e6420cdbcc77693ccdf4e2b2630443dd0e5f760c3c36e84200da20
BLAKE2b-256 checksum
How to use checksums
c2d816fc6530fecdd05b8d517b75356fd0d893364073e84361790c70dee7fcab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.8.5 Linux/4.19.0-20-amd64

Release files / timeoutlib-0.1.0-py3-none-any.whl

Download URL timeoutlib-0.1.0-py3-none-any.whl
Size 3.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
99f79cf2875c2224bde33d57b7ea912fc3a1e3322b81356d6d0bd38e30440547
BLAKE2b-256 checksum
How to use checksums
53a2a0aa9f5da3665ba8167ee336510b86a78f5714c8b358dbefffbfcab5b0b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.8.5 Linux/4.19.0-20-amd64

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page