Python Retrying Library

1.4.2 · maintenance · verified Sat Mar 28

Retrying is an Apache 2.0 licensed general-purpose Python library designed to simplify the task of adding retry behavior to functions. It allows developers to configure retry logic for transient failures, specifying stop conditions, wait strategies (like exponential backoff), and conditions for retrying based on exceptions or return values. The current version is 1.4.2.

Warnings

Install

Imports

Quickstart

Demonstrates how to use the `@retry` decorator with `stop_max_attempt_number` and `wait_fixed` to retry a function that might fail intermittently. By default, `@retry` retries on any exception indefinitely and without delay, so it's crucial to configure stop and wait conditions.

import random
import os
from retrying import retry, stop_after_attempt, wait_fixed

@retry(stop_max_attempt_number=5, wait_fixed=1000) # Retry up to 5 times, waiting 1 second between attempts
def do_something_unreliable():
    if random.randint(0, 10) > 2: # Simulate a failure 70% of the time
        print("Operation failed, retrying...")
        raise IOError("Simulated transient error")
    else:
        print("Operation successful!")
        return "Awesome sauce!"

try:
    result = do_something_unreliable()
    print(f"Final result: {result}")
except IOError as e:
    print(f"Operation failed after multiple retries: {e}")

view raw JSON →