Small library for a neat looking retry wrapper
Project description
# yaretry - Yet Another Retry Library
This is a small python library for wrapping or decorating functions with retrying logic.
## Naive Usage
Wrap any function with the default retrying logic:
```python
import yaretry
...
retrying_f = yaretry.wrap(f)
res = retrying_f(call, with, normal, params)
```
Decorate a function with the default retrying logic:
```python
@yaretry.decorate()
def foo(bar):
pass
foo()
```
The default retrying logic will retry calling the function, as long as it raises an instanceof Exception, and will wait an exponentially growing delay between each call. The initial delay will be 1 second, and will double itself every time.
## Customizing Retrying Logic
The `yaretry` library comes with a Params class, that can be passed to the wrapper and decorator, to customize the retrying logic.
To add a timeout, measured in seconds:
```
retrying_f = yaretry.wrap(f, yaretry.Params(timeout=60))
```
To add a maximum number of retry attempts:
```
retrying_f = yaretry.wrap(f, yaretry.Params(max_attempts=10))
```
To change the initial delay, and the exponent:
```
retrying_f = yaretry.wrap(f, yaretry.Params(intial_delay=0.5, exponent=1.2))
# make the delay constant:
retrying_f = yaretry.wrap(f, yaretry.Params(intial_delay=10, exponent=1))
```
To retry in case the function returns False (or a value that, when cast to bool will be False):
```
@yaretry.decorate(yaretry.Params(retry_on_false=True))
def foo(bar):
if bar == 1:
return False # will retry
elif bar == 2:
return None # will retry
elif bar == 3:
return "False" # will *not* retry
else:
return True
```
To retry only in case of a specific exception or several exceptions:
```
@yaretry.decorate(yaretry.Params(allowed_exceptions=(
MyCustomException,
LookupError))
def foo(bar):
if bar == 1:
raise MyCustomException() # will retry
elif bar == 2:
raise Exception() # will not retry and raise the Exception
else:
raise KeyError() # will retry, as KeyError subclasses LookupError
```
The full list of parameters to customize:
- *timeout* - number of seconds after which retrying will stop
- *max_attempts* - the number of attempts to retry calling the function, before giving up
- *initial_delay* - the number of seconds to wait before the first retry
- *max_delay* - the largest delay to wait between retries
- *exponent* - from retry to retry, the delay is multiplied by this number
- *logger* - use the provided logger to log when a retry is performed
- *raise_last_exception* - if True, after retrying is done, the last exception thrown will be raised. if False (the default), the first exception thrown is raised.
- *retry_on_false* - retry calling the function if it returns a value which `bool(value) == False`
- *allowed_exceptions* - a tuple of exception classes which, when thrown, the function will be retried
- *should_retry_cb* - a callback which will be called with the thrown exception. If the callback returns True, the function will be retried
- *log_level* - a logging.XXXXX log level, indicating which log level to use when logging retry attempts
## Some more examples
Pass a callback to retry only on HTTP 5xx Errors
```python
import yaretry
import requests
def is_server_or_connection_error(ex):
if isinstance(ex, requests.exceptions.HTTPError):
return ex.response.status_code >= 500
if isinstance(ex, requests.exceptions.ConnectionError):
return True
return False
@yaretry.decorate(yaretry.Params(
max_attempts=5,
should_retry_cb=is_server_or_connection_error))
def call_api(url):
res = requests.get(url)
res.raise_on_failure()
return res.json()
```
This is a small python library for wrapping or decorating functions with retrying logic.
## Naive Usage
Wrap any function with the default retrying logic:
```python
import yaretry
...
retrying_f = yaretry.wrap(f)
res = retrying_f(call, with, normal, params)
```
Decorate a function with the default retrying logic:
```python
@yaretry.decorate()
def foo(bar):
pass
foo()
```
The default retrying logic will retry calling the function, as long as it raises an instanceof Exception, and will wait an exponentially growing delay between each call. The initial delay will be 1 second, and will double itself every time.
## Customizing Retrying Logic
The `yaretry` library comes with a Params class, that can be passed to the wrapper and decorator, to customize the retrying logic.
To add a timeout, measured in seconds:
```
retrying_f = yaretry.wrap(f, yaretry.Params(timeout=60))
```
To add a maximum number of retry attempts:
```
retrying_f = yaretry.wrap(f, yaretry.Params(max_attempts=10))
```
To change the initial delay, and the exponent:
```
retrying_f = yaretry.wrap(f, yaretry.Params(intial_delay=0.5, exponent=1.2))
# make the delay constant:
retrying_f = yaretry.wrap(f, yaretry.Params(intial_delay=10, exponent=1))
```
To retry in case the function returns False (or a value that, when cast to bool will be False):
```
@yaretry.decorate(yaretry.Params(retry_on_false=True))
def foo(bar):
if bar == 1:
return False # will retry
elif bar == 2:
return None # will retry
elif bar == 3:
return "False" # will *not* retry
else:
return True
```
To retry only in case of a specific exception or several exceptions:
```
@yaretry.decorate(yaretry.Params(allowed_exceptions=(
MyCustomException,
LookupError))
def foo(bar):
if bar == 1:
raise MyCustomException() # will retry
elif bar == 2:
raise Exception() # will not retry and raise the Exception
else:
raise KeyError() # will retry, as KeyError subclasses LookupError
```
The full list of parameters to customize:
- *timeout* - number of seconds after which retrying will stop
- *max_attempts* - the number of attempts to retry calling the function, before giving up
- *initial_delay* - the number of seconds to wait before the first retry
- *max_delay* - the largest delay to wait between retries
- *exponent* - from retry to retry, the delay is multiplied by this number
- *logger* - use the provided logger to log when a retry is performed
- *raise_last_exception* - if True, after retrying is done, the last exception thrown will be raised. if False (the default), the first exception thrown is raised.
- *retry_on_false* - retry calling the function if it returns a value which `bool(value) == False`
- *allowed_exceptions* - a tuple of exception classes which, when thrown, the function will be retried
- *should_retry_cb* - a callback which will be called with the thrown exception. If the callback returns True, the function will be retried
- *log_level* - a logging.XXXXX log level, indicating which log level to use when logging retry attempts
## Some more examples
Pass a callback to retry only on HTTP 5xx Errors
```python
import yaretry
import requests
def is_server_or_connection_error(ex):
if isinstance(ex, requests.exceptions.HTTPError):
return ex.response.status_code >= 500
if isinstance(ex, requests.exceptions.ConnectionError):
return True
return False
@yaretry.decorate(yaretry.Params(
max_attempts=5,
should_retry_cb=is_server_or_connection_error))
def call_api(url):
res = requests.get(url)
res.raise_on_failure()
return res.json()
```
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
yaretry-0.1.1.tar.gz
(3.4 kB
view details)
Built Distribution
File details
Details for the file yaretry-0.1.1.tar.gz
.
File metadata
- Download URL: yaretry-0.1.1.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ee5d92a7c1115c0b9be75dc30714f97df5deb9cace90fdee878f9c23d5e20f0 |
|
MD5 | fe3a07a9ea9ba1fbea30c6d77f94c237 |
|
BLAKE2b-256 | cdbbdcd98e00b8437bc60377c895cebb93834e855ab79ef45aa3c3c410cdf734 |
File details
Details for the file yaretry-0.1.1-py2.py3-none-any.whl
.
File metadata
- Download URL: yaretry-0.1.1-py2.py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77bf58af7709ccd7162086d8c930f9d71cd4b1506ee0a5912a0d26c860d7aae2 |
|
MD5 | 6fcc63610700ce84e80c1b94226e7689 |
|
BLAKE2b-256 | 07a7ac63681bb0fd4bc95d227f011cd0e3f70b2cead3f50fd11d604f7be69d0a |