Easy to use retry decorator
The `retry` library (version 0.9.2) provides a simple decorator for adding retry logic to Python functions. It allows configuration of exceptions to catch, number of attempts, delay between retries, backoff strategy, and optional jitter. This library is largely unmaintained, with its last release in 2016, and is superseded by more actively developed alternatives like `tenacity`.
Warnings
- breaking This 'retry' library (invl/retry) is largely unmaintained, with its last release in 2016. Its API is not compatible with 'tenacity', which is the actively maintained successor to the 'retrying' library (another distinct, unmaintained project).
- gotcha By default, the `@retry` decorator will retry indefinitely if the `tries` parameter is not explicitly set or set to -1.
- gotcha The `retry` decorator does not inherently support asynchronous functions (async/await syntax).
- gotcha When using `jitter`, if a tuple `(min, max)` is provided, the extra delay is chosen randomly. If a single number is provided, the jitter is fixed.
Install
-
pip install retry
Imports
- retry
from retry import retry
Quickstart
import random
from retry import retry
import logging
logging.basicConfig(level=logging.INFO)
@retry(exceptions=(IOError, ValueError), tries=3, delay=2, backoff=2, max_delay=10)
def unreliable_function():
if random.randint(0, 5) > 2:
raise IOError("Simulated network error")
elif random.randint(0, 5) < 1:
raise ValueError("Simulated data error")
else:
print("Function succeeded!")
return "Success"
try:
result = unreliable_function()
print(f"Final result: {result}")
except (IOError, ValueError) as e:
print(f"Function failed after all retries: {e}")