Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pnpnpn/timeout-decorator
Timeout decorator for Python
https://github.com/pnpnpn/timeout-decorator
decorators python python-decorators timeout timeout-library
Last synced: 2 months ago
JSON representation
Timeout decorator for Python
- Host: GitHub
- URL: https://github.com/pnpnpn/timeout-decorator
- Owner: pnpnpn
- License: mit
- Created: 2012-12-13T18:20:56.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2024-01-12T01:04:32.000Z (about 1 year ago)
- Last Synced: 2024-07-05T16:59:51.959Z (7 months ago)
- Topics: decorators, python, python-decorators, timeout, timeout-library
- Language: Python
- Homepage:
- Size: 43.9 KB
- Stars: 615
- Watchers: 12
- Forks: 96
- Open Issues: 40
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGES.rst
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-python-decorator - timeout-decorator
README
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("{} seconds have passed".format(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("{} seconds have passed".format(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("{} seconds have passed".format(i))if __name__ == '__main__':
mytest().. warning::
Make sure that in case of multiprocessing strategy for timeout, your function does not return objects which cannot
be pickled, otherwise it will fail at marshalling it between master and child processes.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.pyContribute
----------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 `_
.. |Build Status| image:: https://travis-ci.org/pnpnpn/timeout-decorator.svg?branch=master
:target: https://travis-ci.org/pnpnpn/timeout-decorator
.. |Pypi Status| image:: https://badge.fury.io/py/timeout-decorator.svg
:target: https://badge.fury.io/py/timeout-decorator
.. |Coveralls Status| image:: https://coveralls.io/repos/pnpnpn/timeout-decorator/badge.png?branch=master
:target: https://coveralls.io/r/pnpnpn/timeout-decorator