Skip to main content
Yanked

This release has been yanked by its maintainers, and will be ignored by installers, except when explicitly specified.
Consider using release 1.4.2 instead.
Reason given by maintainers: Accidentally broke PY2 compatibility

Retrying

Retrying is an Apache 2.0 licensed general-purpose retrying library, written in Python, to simplify the task of adding retry behavior to just about anything.

The simplest use case is retrying a flaky function whenever an Exception occurs until a value is returned.

import random
from retrying import retry

@retry
def do_something_unreliable():
    if random.randint(0, 10) > 1:
        raise IOError("Broken sauce, everything is hosed!!!111one")
    else:
        return "Awesome sauce!"

print do_something_unreliable()

Features

  • Generic Decorator API

  • Specify stop condition (i.e. limit by number of attempts)

  • Specify wait condition (i.e. exponential backoff sleeping between attempts)

  • Customize retrying on Exceptions

  • Customize retrying on expected returned result

Examples

As you saw above, the default behavior is to retry forever without waiting.

@retry
def never_give_up_never_surrender():
    print "Retry forever ignoring Exceptions, don't wait between retries"

Let’s be a little less persistent and set some boundaries, such as the number of attempts before giving up.

@retry(stop_max_attempt_number=7)
def stop_after_7_attempts():
    print "Stopping after 7 attempts"

We don’t have all day, so let’s set a boundary for how long we should be retrying stuff.

@retry(stop_max_delay=10000)
def stop_after_10_s():
    print "Stopping after 10 seconds"

Most things don’t like to be polled as fast as possible, so let’s just wait 2 seconds between retries.

@retry(wait_fixed=2000)
def wait_2_s():
    print "Wait 2 second between retries"

Some things perform best with a bit of randomness injected.

@retry(wait_random_min=1000, wait_random_max=2000)
def wait_random_1_to_2_s():
    print "Randomly wait 1 to 2 seconds between retries"

Then again, it’s hard to beat exponential backoff when retrying distributed services and other remote endpoints.

@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000)
def wait_exponential_1000():
    print "Wait 2^x * 1000 milliseconds between each retry, up to 10 seconds, then 10 seconds afterwards"

We have a few options for dealing with retries that raise specific or general exceptions, as in the cases here.

def retry_if_io_error(exception):
    """Return True if we should retry (in this case when it's an IOError), False otherwise"""
    return isinstance(exception, IOError)

@retry(retry_on_exception=retry_if_io_error)
def might_io_error():
    print "Retry forever with no wait if an IOError occurs, raise any other errors"

@retry(retry_on_exception=retry_if_io_error, wrap_exception=True)
def only_raise_retry_error_when_not_io_error():
    print "Retry forever with no wait if an IOError occurs, raise any other errors wrapped in RetryError"

We can also use the result of the function to alter the behavior of retrying.

def retry_if_result_none(result):
    """Return True if we should retry (in this case when result is None), False otherwise"""
    return result is None

@retry(retry_on_result=retry_if_result_none)
def might_return_none():
    print "Retry forever ignoring Exceptions with no wait if return value is None"

Any combination of stop, wait, etc. is also supported to give you the freedom to mix and match.

Contribute

  1. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug.

  2. Fork the repository on GitHub to start making your changes to the master branch (or branch off of it).

  3. Write a test which shows that the bug was fixed or that the feature works as expected.

  4. Send a pull request and bug the maintainer until it gets merged and published. :) Make sure to add yourself to AUTHORS.

History

1.3.5 (2025-06-22)

  • Remove six dependency

  • Add basic logging support

  • Fix tests and add GHA

  • Accept Exception type for retry_on_exception

1.3.4 (2022-09-03)

  • Added Greg Roodt as maintainer

  • Formatted code with black

  • Updated repository references

1.3.3 (2014-12-14)

  • Add minimum six version of 1.7.0 since anything less will break things

1.3.2 (2014-11-09)

  • Ensure we wrap the decorated functions to prevent information loss

  • Allow a jitter value to be passed in

1.3.1 (2014-09-30)

  • Add requirements.txt to MANIFEST.in to fix pip installs

1.3.0 (2014-09-30)

  • Add upstream six dependency, remove embedded six functionality

1.2.3 (2014-08-25)

  • Add support for custom wait and stop functions

1.2.2 (2014-06-20)

  • Bug fix to not raise a RetryError on failure when exceptions aren’t being wrapped

1.2.1 (2014-05-05)

  • Bug fix for explicitly passing in a wait type

1.2.0 (2014-05-04)

  • Remove the need for explicit specification of stop/wait types when they can be inferred

  • Add a little checking for exception propagation

1.1.0 (2014-03-31)

  • Added proper exception propagation through reraising with Python 2.6, 2.7, and 3.2 compatibility

  • Update test suite for behavior changes

1.0.1 (2013-03-20)

  • Fixed a bug where classes not extending from the Python exception hierarchy could slip through

  • Update test suite for custom Python exceptions

1.0.0 (2013-01-21)

  • First stable, tested version now exists

  • Apache 2.0 license applied

  • Sanitizing some setup.py and test suite running

  • Added Travis CI support

Metadata

Release files for retrying 1.3.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for retrying 1.3.5
File Size Uploaded Provenance Actions
retrying-1.3.5.tar.gz 11.1 kB Without provenance Details for retrying-1.3.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for retrying 1.3.5
File Interpreter ABI Platform Actions
retrying-1.3.5-py3-none-any.whl Python 3 none any Details for retrying-1.3.5-py3-none-any.whl

Total release size: 23.0 kB

Release files / retrying-1.3.5.tar.gz

Download URL retrying-1.3.5.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
13859d82b786d6d76df33d59a2372623deed45d57028018092dec612c45064d8
BLAKE2b-256 checksum
How to use checksums
1b680bcc52d0476fcb607bd9a815a2d4ae7c643e1651221444a42d046197b37e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.6

Release files / retrying-1.3.5-py3-none-any.whl

Download URL retrying-1.3.5-py3-none-any.whl
Size 11.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8b5093743188752de37c5e6b21485d87c0b0762b51947ac66658863643ce1d8d
BLAKE2b-256 checksum
How to use checksums
60928dc89f7dcf3bbf170df068a70eb2e6ac07f5b05439b9eea3660336b978f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.6

Security for retrying 1.3.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page