Skip to main content

Timeout library for generic interruption of main thread by an exception after a configurable duration

Project description

Timeout library for generic interruption of main thread by an exception after a configurable duration.

Use case

Set a timeout (seconds) upon expiration of which an exception is raised:

from timeout import timeout

…as a context manager:

with timeout(0.5):
    ... work ...

…or decorating a function:

@timeout(0.5)
def work():
    ...

The exception may be configured, either by passing an exception instance, or by specifying an overriding exception class and/or value(s):

with timeout(0.5, RuntimeError("Work took too long")):
    ... work ...

with timeout(0.5, RuntimeError, "Work took too long"):
    ... work ...

with timeout(0.5, exc=RuntimeError):
    ... work ...

with timeout(0.5, value="Work took too long"):
    ... work ...

with timeout(0.5, value=(2, "Took too long", 'work.py')):
    ... work ...

The timeout exception defaults to TimeoutError("Operation timed out").

Note: timeout is implemented via signal, and as such may not be applied outside of a process’s main thread.

Installation

signalled-timeout is a Python distribution, which may be installed via easy_install or pip, e.g.:

pip install signalled-timeout

…or, from source:

python setup.py install

Testing

To test the distribution on your system, execute its test command:

python setup.py test

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

signalled-timeout-1.0.0.tar.gz (2.7 kB view details)

Uploaded Source

File details

Details for the file signalled-timeout-1.0.0.tar.gz.

File metadata

File hashes

Hashes for signalled-timeout-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7fa88a7083a69e4c38b5f26e191a101a052dd39c85f5e79cbfb3ee38efb084f9
MD5 2d7648010651900feeea6b35c62cf381
BLAKE2b-256 98d10feeeb2ea3ca26388e06e1c4637fa4915519242a1f1a7a84076e60867d65

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