Retrying
Low maintenance activity. Last release 16 months ago, commits in 2 of the last 26 weeks. Weaker on commit activity and recent releases.
Consider insteadpython-backoffPythie’s Index: 82523.3K downloads/moCompare →
pip install retryingRetrying
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")