skip to navigation
skip to content

interruptingcow 0.6

A watchdog that interrupts long running code.

Interruptingcow is a generic utility can relatively gracefully interrupt your Python code when it doesn’t execute within a specific number of seconds:

from interruptingcow import timeout

    with timeout(5, exception=RuntimeError):
        # perform a potentially very slow operation
except RuntimeError:
    print "didn't finish within 5 seconds"

Timeouts are specified in seconds (as floats with theoretical microsecond precision).


$ pip install interruptingcow


Interruptingcow is fully reentrant, which means that you can have nested timeouts:

from interruptingcow import timeout

class Outer(RuntimeError): pass

class Inner(RuntimeError): pass

    with timeout(20.0, Outer):
            with timeout(1.0, Inner):
                # some expensive operation
        except Inner:

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

def foo():
    with timeout(.3):
        # some expensive operation


Interruptingcow uses signal(SIGALRM) to let the operating system interrupt program execution. This has the following limitations:

  1. Python signal handlers only apply to the main thread, so you cannot use this from other threads
  2. You must not use this in a program that uses SIGALRM itself (this includes certain profilers)
File Type Py Version Uploaded on Size
interruptingcow-0.6.tar.gz (md5) Source 2012-06-21 3KB
  • Downloads (All Versions):
  • 62 downloads in the last day
  • 789 downloads in the last week
  • 2272 downloads in the last month