Skip to main content

Timeout decorator

Project description

Timeout decorator

Build Status Pypi Status Coveralls Status

Installation

From source code:

python setup.py install

From pypi:

pip install timeout-decorator

Usage

import time
import timeout_decorator

@timeout_decorator.timeout(5)
def mytest():
    print "Start"
    for i in range(1,10):
        time.sleep(1)
        print "%d seconds have passed" % i

if __name__ == '__main__':
    mytest()

Specify an alternate exception to raise on timeout:

import time
import timeout_decorator

@timeout_decorator.timeout(5, timeout_exception=StopIteration)
def mytest():
    print "Start"
    for i in range(1,10):
        time.sleep(1)
        print "%d seconds have passed" % i

if __name__ == '__main__':
    mytest()

Multithreading

By default, timeout-decorator uses signals to limit the execution time of the given function. This appoach does not work if your function is executed not in a main thread (for example if it’s a worker thread of the web application). There is alternative timeout strategy for this case - by using multiprocessing. To use it, just pass use_signals=False to the timeout decorator function:

import time
import timeout_decorator

@timeout_decorator.timeout(5, use_signals=False)
def mytest():
    print "Start"
    for i in range(1,10):
        time.sleep(1)
        print "%d seconds have passed" % i

if __name__ == '__main__':
    mytest()

Acknowledgement

Derived from http://www.saltycrane.com/blog/2010/04/using-python-timeout-decorator-uploading-s3/ and https://code.google.com/p/verse-quiz/source/browse/trunk/timeout.py

Contribute

I would love for you to fork and send me pull request for this project. Please contribute.

License

This software is licensed under the MIT license

See License file

Changelog

0.3.1

  • Fixed issue with PicklingError causes the timeout to never be reached.

0.3.0

  • Added optional threading support via python multiprocessing (bubenkoff)

  • Switched to pytest test runner (bubenkoff)

0.2.1

  • Initial public release

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

timeout-decorator-0.4.0.tar.gz (4.7 kB view details)

Uploaded Source

File details

Details for the file timeout-decorator-0.4.0.tar.gz.

File metadata

File hashes

Hashes for timeout-decorator-0.4.0.tar.gz
Algorithm Hash digest
SHA256 e49d214b17a096ca5162865ccb79f516840e5ba21b394f531f1f1d50e2e293ad
MD5 6ef20407e517a80055844e68dfe34af6
BLAKE2b-256 a2bcbc7395efa9212c5507c489cd348ad50ac437dbfc94fa5d88f87e58f9fa3c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page