Easy-to-Use Retry Decorator

0.9.5 · active · verified Sat Apr 11

retry2 is a Python library that provides an easy-to-use decorator for retrying functions. It is a fork of the unmaintained `invl/retry` library, offering similar functionality with a focus on being pure standard library Python, though it can optionally preserve function signatures with an additional `decorator` dependency. The current version is 0.9.5.

Warnings

Install

Imports

Quickstart

This example demonstrates using the `@retry` decorator to automatically retry a function that raises an `IOError`. It will attempt 3 times, with an initial 1-second delay that doubles (backoff=2) between attempts.

from retry import retry
import time

call_count = 0

@retry(exceptions=IOError, tries=3, delay=1, backoff=2)
def might_fail_api_call():
    global call_count
    call_count += 1
    print(f"Attempt {call_count}: Calling API...")
    if call_count < 3:
        raise IOError("Temporary network issue")
    print("API call successful!")
    return "Data"

try:
    result = might_fail_api_call()
    print(f"Final result: {result}")
except IOError as e:
    print(f"Failed after multiple retries: {e}")

view raw JSON →