Skip to main content

Decorators for functions that you wish to retry

Project description

# trythatagain

[![Build Status](](

This provides decorators for super simple function/method call retries when an exception is raised.

## Installation

pip install trythatagain

## Examples

Here's a simple use case: querying the AWS API, knowing you might hit API limits.

from trythatagain import retry

def find_ebs_volumes(unattached=True, no_snapshots=True):

except CaughtException as e:
print('Failed to list EBS volumes')

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 `__cause__`.

Retry as many times as times as necessary:

from trythatagain import retry

def try_five_times():
raise Exception('This always fails')

def retry_forever():
raise Exception('Terrible waste of CPU cycles')

Raise immediately on specific exceptions:

def update_cache():

Suppress re-raising exceptions:

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.

# waits 1 second, then 4, then 9, etc.
def update_instance_tags():

def scrape_url():

Custom wait functions are also possible:

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.

Filename, size & hash SHA256 hash help File type Python version Upload date
trythatagain-0.1.3-py2-none-any.whl (4.1 kB) Copy SHA256 hash SHA256 Wheel 2.7
trythatagain-0.1.3-py3-none-any.whl (4.1 kB) Copy SHA256 hash SHA256 Wheel 3.6
trythatagain-0.1.3.tar.gz (3.4 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page