Decorators for functions that you wish to retry
Project description
# trythatagain
This provides decorators for super simple function/method call retries when an exception is raised.
## Installation
```bash
pip install trythatagain
```
## Examples
Here's a simple use case: querying the AWS API, knowing you might hit API limits.
```python
from trythatagain import retry
@retry
def find_ebs_volumes(unattached=True, no_snapshots=True):
...
try:
find_ebs_volumes()
except CaughtException as e:
print('Failed to list EBS volumes')
print(e.caught_exception)
```
This will retry calling `func` three times. If was not successful in calling the function without an exception, it will re-raise the exception as `CaughtException`, with the original exception available at the attribute `caught_exception`.
Retry as many times as times as necessary:
```python
from trythatagain import retry
@retry(retries=5)
def try_five_times():
raise Exception('This always fails')
@retry(retries=0)
def retry_forever():
raise Exception('Terrible waste of CPU cycles')
```
Raise immediately on specific exceptions:
```python
@retry(raise_for=ValueError)
def update_cache():
...
```
Suppress re-raising exceptions:
```python
@retry(reraise=False)
def reload_user_data():
...
```
There's also exponential and linear backoff retries things like cooling down after hitting API limits. In fact, AWS recommends exponential backoff to deal with API limits.
```python
# waits 1 second, then 4, then 9, etc.
@retry_exp_backoff(unit=MILLISECONDS)
def update_instance_tags():
...
@retry_linear_backoff(unit=SECONDS)
def scrape_url():
...
```
Custom wait functions are also possible:
```python
def fixed(i, unit):
time.sleep(5 * unit)
@retry(wait_func=fixed, unit=MILLISECONDS)
def func():
...
```
This provides decorators for super simple function/method call retries when an exception is raised.
## Installation
```bash
pip install trythatagain
```
## Examples
Here's a simple use case: querying the AWS API, knowing you might hit API limits.
```python
from trythatagain import retry
@retry
def find_ebs_volumes(unattached=True, no_snapshots=True):
...
try:
find_ebs_volumes()
except CaughtException as e:
print('Failed to list EBS volumes')
print(e.caught_exception)
```
This will retry calling `func` three times. If was not successful in calling the function without an exception, it will re-raise the exception as `CaughtException`, with the original exception available at the attribute `caught_exception`.
Retry as many times as times as necessary:
```python
from trythatagain import retry
@retry(retries=5)
def try_five_times():
raise Exception('This always fails')
@retry(retries=0)
def retry_forever():
raise Exception('Terrible waste of CPU cycles')
```
Raise immediately on specific exceptions:
```python
@retry(raise_for=ValueError)
def update_cache():
...
```
Suppress re-raising exceptions:
```python
@retry(reraise=False)
def reload_user_data():
...
```
There's also exponential and linear backoff retries things like cooling down after hitting API limits. In fact, AWS recommends exponential backoff to deal with API limits.
```python
# waits 1 second, then 4, then 9, etc.
@retry_exp_backoff(unit=MILLISECONDS)
def update_instance_tags():
...
@retry_linear_backoff(unit=SECONDS)
def scrape_url():
...
```
Custom wait functions are also possible:
```python
def fixed(i, unit):
time.sleep(5 * unit)
@retry(wait_func=fixed, unit=MILLISECONDS)
def func():
...
```
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
trythatagain-0.1.2.tar.gz
(3.3 kB
view hashes)
Built Distributions
Close
Hashes for trythatagain-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cff93668edf277c877b458ee6d5cc1bb7b14761a02c5368c81d54f7f4fd9be76 |
|
MD5 | 4f782a1ae736e96bda6d8c0965c054b4 |
|
BLAKE2b-256 | d2ab23a55c2e65c6427a419ff599ad591c93d30cd6c890b5ecc8c707fef0b1a5 |
Close
Hashes for trythatagain-0.1.2-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc40df6be3ddb3eb9460c1a97812ae4c965a971cd0a2d60a16ce77573d4d6c11 |
|
MD5 | 132f407279967ea79c4de4118cc834cb |
|
BLAKE2b-256 | 363ef6c79d6cf2ebda6aad194a9eac64854f9d6c5ef6b05a1febe5f93b1e9459 |