Retry Decorator

1.1.1 · active · verified Wed Apr 15

The `retry-decorator` library provides a simple yet powerful decorator for retrying function calls upon specified exceptions. It supports features like setting maximum retries, exponential backoff, and logging failed attempts. Version 1.1.1 is the latest release, with future versions planned to drop Python 2 support. The project appears to be actively maintained, though releases are infrequent.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use the `@retry` decorator with explicit exception handling, a maximum number of retries, and a basic backoff factor. It also configures a logger to show retry attempts.

import logging
from python_retry import retry

LOGGER = logging.getLogger(__name__)

@retry(max_retries=3,
       backoff_factor=1,
       retry_on=(ValueError,),
       retry_logger=LOGGER)
def flaky_function():
    print("Attempting flaky operation...")
    if hasattr(flaky_function, 'calls'):
        flaky_function.calls += 1
    else:
        flaky_function.calls = 1

    if flaky_function.calls < 3:
        raise ValueError("Simulated transient error")
    print("Operation successful!")
    return "Success Data"

if __name__ == "__main__":
    try:
        result = flaky_function()
        print(f"Final result: {result}")
    except ValueError as e:
        print(f"Operation failed after multiple retries: {e}")

view raw JSON →