dssg/signalled-timeout

Name: signalled-timeout

Owner: Data Science for Social Good

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

Created: 2017-11-08 18:04:56.0

Updated: 2017-11-08 21:26:14.0

Pushed: 2017-11-08 21:50:51.0

Homepage:

Size: 4

Language: Python

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

signalled-timeout

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

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.