Easy to use retry decorator

0.9.2 · abandoned · verified Sat Mar 28

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

Install

Imports

Quickstart

This quickstart demonstrates how to apply the `@retry` decorator to a function, configuring it to retry on specific exceptions, with a maximum number of attempts, an initial delay, exponential backoff, and a maximum delay between retries. It will print logging messages for each retry attempt.

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}")

view raw JSON →