Interrupting Cow
Interruptingcow is a generic utility that can relatively gracefully interrupt your Python code when it doesn’t execute within a specific number of seconds:
from interruptingcow import timeout
try:
with timeout(5, exception=RuntimeError):
# perform a potentially very slow operation
pass
except RuntimeError:
print "didn't finish within 5 seconds"
Timeouts are specified in seconds (as floats with theoretical microsecond precision).
Installation
$ pip install interruptingcow
Reentrant
Interruptingcow is fully reentrant, which means that you can have nested timeouts:
from interruptingcow import timeout
class Outer(RuntimeError): pass
class Inner(RuntimeError): pass
try:
with timeout(20.0, Outer):
try:
with timeout(1.0, Inner):
# some expensive operation
try_the_expensive_thing()
except Inner:
do_the_cheap_thing_instead()
except Outer:
print 'Program as a whole failed to return in 20 secs'
Nested timeouts allow a large outer timeout to contain smaller timeouts. If the inner timeout is larger than the outer timeout, it is treated as a no-op.
Function Decorators
Interruptingcow can be used both as inline with-statements, as shown in the above examples, as well as function decorator:
from interruptingcow import timeout
@timeout(.5)
def foo():
with timeout(.3):
# some expensive operation
pass
Quotas
You can allocate a quota of time and then share it across multiple invocations to timeout(). This is especially useful if you need to use timeouts inside a loop:
from interruptingcow import timeout, Quota
quota = Quota(1.0)
for i in something:
try:
with timeout(quota, RuntimeError):
# perform a slow operation
pass
except RuntimeError:
# do a cheaper thing instead
Here the first iterations of the loop will be able to perform the expensive operation, until the shared quota of 1 second runs out and then the remaining iterations will perform the cheaper alternative.
A single quota instance can also be shared across all calls to timeout() your application makes (including nested calls), to give place an upper bound on the total runtime, regardless of how many calls to timeout() you have.
Caveats
Interruptingcow uses signal(SIGALRM) to let the operating system interrupt program execution. This has the following limitations:
Python signal handlers only apply to the main thread, so you cannot use this from other threads
You must not use this in a program that uses SIGALRM itself (this includes certain profilers)
Metadata
Release files for interruptingcow 0.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| interruptingcow-0.8.tar.gz | 5.0 kB | Without provenance | Details |
Release files / interruptingcow-0.8.tar.gz
| Download URL | interruptingcow-0.8.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3e8cd5058b651e625702cba53e3b1fb76d7a5ec07ab69c52a167a9f784e3306c
|
|
BLAKE2b-256 checksum How to use checksums |
643a1e58f9e38acb38d42584872150400ddafb6d669700fd9b99f079adce00f6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for interruptingcow 0.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.