https://github.com/dssg/signalled-timeout
Timeout library for generic interruption of main thread by an exception after a configurable duration.
https://github.com/dssg/signalled-timeout
programming-utility
Last synced: 6 months ago
JSON representation
Timeout library for generic interruption of main thread by an exception after a configurable duration.
- Host: GitHub
- URL: https://github.com/dssg/signalled-timeout
- Owner: dssg
- Created: 2017-11-08T18:04:56.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-08T21:50:51.000Z (over 8 years ago)
- Last Synced: 2024-08-14T06:56:07.299Z (almost 2 years ago)
- Topics: programming-utility
- Language: Python
- Homepage:
- Size: 3.91 KB
- Stars: 2
- Watchers: 9
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
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