A Python decorator for retrying functions with exponential backoff, optional jitter, and support for both sync and async code.
Project description
🌀 Retry Tools
A Python decorator for retrying functions with exponential backoff, optional jitter, and full support for both synchronous and asynchronous code.
🔍 About
This library provides a simple yet powerful @retry decorator that retries a function when specified exceptions are raised. It supports:
- Synchronous and asynchronous functions
- Exponential backoff with optional jitter
- Custom exception handling
- Logging or custom log output
Useful for handling flaky APIs, intermittent database errors, or any transient issues where retrying helps.
📦 Installation
pip install retrytools
🚀 Quick Start
Basic Usage
from retrytools import retry
@retry(catch_errors=ValueError, tries=3, delay=1)
def flaky_function():
# Simulates an error-prone operation
if random.random() < 0.7:
raise ValueError("Transient issue!")
return "Success!"
result = flaky_function()
print(result)
With Async Function
import asyncio
from retrytools import retry
@retry(catch_errors=ConnectionError, tries=5, delay=0.5)
async def unstable_async_api():
if random.random() < 0.5:
raise ConnectionError("Temporary network glitch")
return "Fetched!"
asyncio.run(unstable_async_api())
⚙️ Parameters
| Argument | Type | Description |
|---|---|---|
catch_errors |
Type[Exception] or tuple |
Exception(s) to retry on |
tries |
int |
Max attempts (default: 3) |
delay |
float |
Initial delay in seconds |
throw_error |
Exception (optional) |
Custom error to raise after final failure |
logger |
Logger or Callable[[str]] |
Logging handler (e.g., print or logging.Logger) |
jitter |
bool or float |
Random delay variation: True for full jitter, float for additive |
📝 Example with Logging
import logging
from retrytools import retry
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
@retry(catch_errors=(RuntimeError, ConnectionError), tries=4, throw_error=Exception("Custom"), delay=2, logger=logger, jitter=True)
def sometimes_fails():
if random.randint(0, 1):
raise RuntimeError("Oops!")
return "Got it!"
sometimes_fails()
📌 Notes
- Jitter is helpful to avoid retry storms in distributed systems.
- Supports Python 3.7+.
- Works transparently for both sync and async code.
🛠️ License
MIT License
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
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 retrytools-0.0.3.post1.tar.gz.
File metadata
- Download URL: retrytools-0.0.3.post1.tar.gz
- Upload date:
- Size: 16.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e798f88678c390f88d02f9d565ef0456f98a477075b6a691ab7e9ad9790a7638
|
|
| MD5 |
fc1fa25a6761552eb6120b0679bcd891
|
|
| BLAKE2b-256 |
25258fc77eacab6a162b7132a1495f0919c0cd6b26a1e5546c8703a50c795e00
|
File details
Details for the file retrytools-0.0.3.post1-py3-none-any.whl.
File metadata
- Download URL: retrytools-0.0.3.post1-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5096ab9d26386b7af0aac0c4c1dc121e2d1ab168ad98dd86f3d725fd7037f75c
|
|
| MD5 |
e617e8a7b0769db40007e2cdb8326ae3
|
|
| BLAKE2b-256 |
e2f8f78be878381af5a0598431379f769ea171de8e2c1228ba85ce321e09abe5
|