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
@retryto 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
Release history Release notifications | RSS feed
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)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d08a99b2dc1256d97d400a927ae6ba57198e5cfee64432c1a91a23386c2d032
|
|
| MD5 |
0db30dd67b11f168faae08ecdebd8e32
|
|
| BLAKE2b-256 |
d802df28c4491f7cce7431ff8ea4b05cef0b26265d357a4f8aacb3d2796e9369
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b77de4427b4d2f2420792ccf8a3eb8963f3d4865d43fdc5505c94fb76b614231
|
|
| MD5 |
b8986339798bacd21c778766e5929a50
|
|
| BLAKE2b-256 |
a94944bb54d5966ebe47d6d12f4f7237e124574155d8143f42edff2462ff9d1b
|