Skip to main content

A lightweight, beginner-friendly Python retry decorator with exponential backoff.

Project description

ezretry 🔁

A lightweight, beginner-friendly Python decorator to automatically retry failing functions with exponential backoff.


Features

  • 🔁 Simple Decorator Syntax: Just add @retry to any function.
  • 📈 Exponential Backoff: Configurable backoff multiplier to wait longer between retries.
  • 🎯 Targeted Exceptions: Catch specific errors and let other bugs crash normally.
  • 📝 Preserved Signatures: Retains name, docstring, and arguments using functools.wraps.
  • 🪶 Zero Dependencies: Pure Python, standard library only.

Installation

Install locally for development:

pip install -e .

Usage

Basic Example (Retry on any error)

from ezretry import retry

@retry(tries=3)
def fetch_data():
    print("Trying to fetch...")
    # This will be retried up to 3 times if it fails
    return get_api_data()

Advanced Example (Exponential backoff & logging)

import logging
from ezretry import retry

# Set up logging to see retry warnings
logging.basicConfig(level=logging.INFO)
my_logger = logging.getLogger("app")

# Retries up to 5 times.
# Wait times: 1.0s, 2.0s, 4.0s, 8.0s...
@retry(
    exceptions=(ConnectionError, TimeoutError),
    tries=5,
    delay=1.0,
    backoff=2.0,
    logger=my_logger
)
def upload_data():
    print("Uploading...")
    raise ConnectionError("Server unavailable")

Parameters

Parameter Type Default Description
exceptions Exception or tuple Exception The exception types to catch and retry on.
tries int 3 Maximum number of attempts (must be $\ge 1$).
delay float 1.0 Initial delay between retries in seconds.
backoff float 2.0 Multiplier applied to the delay after each failure.
logger logging.Logger None Optional logger to print warnings before sleeping.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ezretry_asik-0.1.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ezretry_asik-0.1.0-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file ezretry_asik-0.1.0.tar.gz.

File metadata

  • Download URL: ezretry_asik-0.1.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for ezretry_asik-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0d08a99b2dc1256d97d400a927ae6ba57198e5cfee64432c1a91a23386c2d032
MD5 0db30dd67b11f168faae08ecdebd8e32
BLAKE2b-256 d802df28c4491f7cce7431ff8ea4b05cef0b26265d357a4f8aacb3d2796e9369

See more details on using hashes here.

File details

Details for the file ezretry_asik-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ezretry_asik-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for ezretry_asik-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b77de4427b4d2f2420792ccf8a3eb8963f3d4865d43fdc5505c94fb76b614231
MD5 b8986339798bacd21c778766e5929a50
BLAKE2b-256 a94944bb54d5966ebe47d6d12f4f7237e124574155d8143f42edff2462ff9d1b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page