retry decorator
Project description
retry is a decorator for isolating retrying logic, with logging intergraton.
Installation
$ pip install retry
API
def retry(exceptions=Exception, tries=float('inf'), delay=0, backoff=1, logger=logging.getLogger(__name__)):
"""Return a decorator for retrying.
:param exceptions: an exception or a tuple of exceptions to catch
:param tries: the maximum number of attempts
:param delay: how many seconds to wait between attmpts
:param backoff: delay growth factor
:param logger: logger.warning(fmt, error, delay) will be called on failed attempts
"""
various retrying logic can be achieved by combination of arguments.
Examples
from retry import retry
# Retry until succeed
@retry()
def make_trouble():
...
# Retry on ZeroDivisionError, fail after 3 attmpts, sleep 2 seconds per
# attmpt
@retry(ZeroDivisionError, tries=3, delay=2)
def make_trouble():
...
# Retry on ValueError and TypeError, sleep 1, 2, 4, 8, etc.. seconds
@retry((ValueError, TypeError), delay=1, backoff=2)
def make_trouble():
...
# If you enable logging, you can get warnings like 'ValueError, retrying in
# 1 seconds'
if __name__ == '__main__':
import logging
logging.basicConfig()
make_trouble()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
retry-0.5.0.tar.gz
(2.3 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
retry-0.5.0-py2-none-any.whl
(4.2 kB
view details)
File details
Details for the file retry-0.5.0.tar.gz.
File metadata
- Download URL: retry-0.5.0.tar.gz
- Upload date:
- Size: 2.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af2f2c4d3d1f360f30b607acb75dd068a652a6f79a5cb83dc4b0b85bfea242d8
|
|
| MD5 |
140f06272fdfb703b5d994d675eb4ca9
|
|
| BLAKE2b-256 |
9766d5b50b6d27baec8673fae7e6eb645a57ade35ff8e6889cb7668a817fd8d6
|
File details
Details for the file retry-0.5.0-py2-none-any.whl.
File metadata
- Download URL: retry-0.5.0-py2-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e3f32d1252a1688be337c5742242372cedaa2a15f99aa410f59921086b2e48e
|
|
| MD5 |
7dac783b469eccc57139eb2250274573
|
|
| BLAKE2b-256 |
6b031521619516383ce34baf277929119780c247cb7420b6b32f4b1d4cc6696e
|