PythonhardPython

Retry Decorator with Backoff

Automatically retry failed operations with exponential backoff.

01

The problem

You need to automatically retry operations that might fail temporarily (API calls, database connections).

02

The solution

Python
import time
import functools
from typing import Tuple, Type

def retry(
    max_attempts: int = 3,
    delay: float = 1.0,
    backoff: float = 2.0,
    exceptions: Tuple[Type[Exception], ...] = (Exception,)
):
    """Retry decorator with exponential backoff."""
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            current_delay = delay
            last_exception = None
            
            for attempt in range(max_attempts):
                try:
                    return func(*args, **kwargs)
                except exceptions as e:
                    last_exception = e
                    if attempt < max_attempts - 1:
                        time.sleep(current_delay)
                        current_delay *= backoff
            
            raise last_exception
        return wrapper
    return decorator

03

Parameters

max_attemptsint

Maximum number of retry attempts

delayfloat

Initial delay between retries in seconds

backofffloat

Multiplier for delay after each retry

exceptionstuple

Exception types to catch and retry

04

Put it to work

Example
import requests

@retry(max_attempts=3, delay=1, exceptions=(requests.RequestException,))
def fetch_data(url):
    response = requests.get(url, timeout=5)
    response.raise_for_status()
    return response.json()

# Will retry up to 3 times on network errors
data = fetch_data('https://api.example.com/data')