Skip to main content

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()

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

Release files for timeout-decorator 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for timeout-decorator 0.3.2
File Size Uploaded
timeout-decorator-0.3.2.tar.gz 4.5 kB Details

Release files / timeout-decorator-0.3.2.tar.gz

Download URL timeout-decorator-0.3.2.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4ee19ecbff3948ef1d2321fc08a0af7ab5a81825b116589a70519cd9f84234f5
BLAKE2b-256 checksum
How to use checksums
5cdfdd12f1f1669571e5f0c63b577a46f4d28f7893c386a486ca2a14470746dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.5.0

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.3

1 release file

This release

0.3.2 This release

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page