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.

Files for trythatagain, version 0.1.3
Filename, size File type Python version Upload date Hashes
Filename, size trythatagain-0.1.3-py2-none-any.whl (4.1 kB) File type Wheel Python version 2.7 Upload date Hashes View
Filename, size trythatagain-0.1.3-py3-none-any.whl (4.1 kB) File type Wheel Python version 3.6 Upload date Hashes View
Filename, size trythatagain-0.1.3.tar.gz (3.4 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page