A universal smart retry library for Python (sync + async)
Project description
🚀 Smart Retry
A lightweight, production-ready retry library for Python. Smart Retry simplifies error handling with a clean, decorator-based API supporting both synchronous and asynchronous execution.
📖 Table of Contents
- Why Smart Retry?
- Features
- Installation
- Quick Start
- Advanced Configuration
- Lifecycle Hooks
- Manual Engine Usage
- API Reference
- Contributing
- License
🤔 Why Smart Retry?
Building resilient systems requires robust error handling. Smart Retry takes the boilerplate out of retry logic:
- Zero Dependencies: Keep your project lean.
- Unified API: One decorator for both
defandasync def. - Extensible: Easily plug in custom backoff strategies and lifecycle hooks.
- Type Safe: Designed with modern Python standards in mind.
✨ Features
- 🔄 Sync & Async: Seamlessly works with traditional and coroutine functions.
- 📉 Backoff Strategies: Built-in Exponential, Linear, and Fixed backoff.
- 🎯 Selective Retries: Target specific exceptions.
- 🪝 Observability: Hooks for
on_retry,on_success, andon_fail. - 🛠️ Developer Friendly: Minimalist API with powerful defaults.
📥 Installation
pip install smart-retry
For development:
git clone https://github.com/ishtiaqmahmood/smart-retry.git
cd smart-retry
pip install -e .
⚡ Quick Start
Basic Decorator
from smart_retry import retry
@retry(max_attempts=3)
def unreliable_service():
# This will be retried up to 3 times on any Exception
return perform_network_call()
Async Support
import asyncio
from smart_retry import retry
@retry(max_attempts=5)
async def fetch_api_data():
return await async_client.get("https://api.example.com")
⚙️ Advanced Configuration
Targeted Exceptions
Only retry when specific errors occur:
@retry(
max_attempts=5,
exceptions=(ConnectionError, TimeoutError)
)
def stable_task():
pass
Custom Backoff
Choose from built-in strategies or provide your own:
from smart_retry.backoff import linear_backoff, fixed_backoff
@retry(backoff=linear_backoff)
def linear_task():
pass
@retry(backoff=fixed_backoff)
def fixed_task():
pass
🪝 Lifecycle Hooks
Monitor and react to the retry lifecycle:
def on_retry(error, attempt):
logging.warning(f"Attempt {attempt} failed: {error}")
@retry(max_attempts=3, on_retry=on_retry)
def monitored_task():
pass
🛠️ Manual Engine Usage
For more control, use the RetryEngine directly:
from smart_retry import RetryEngine
engine = RetryEngine(max_attempts=3)
# Synchronous execution
result = engine.run_sync(my_func, arg1, kwarg1=val)
# Asynchronous execution
result = await engine.run_async(my_async_func)
📚 API Reference
retry Decorator
| Parameter | Type | Default | Description |
|---|---|---|---|
max_attempts |
int |
3 |
Maximum number of attempts. |
exceptions |
tuple |
(Exception,) |
Exception types to catch. |
backoff |
callable |
exponential_backoff |
Delay strategy function. |
on_retry |
callable |
None |
callback(error, attempt) |
on_success |
callable |
None |
callback(result) |
on_fail |
callable |
None |
callback(error) |
🤝 Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
📄 License
Distributed under the MIT License. See LICENSE for more information.
👤 Author
Ishtiaq Mahmood - GitHub
Project Link: https://github.com/ishtiaqmahmood/smart-retry
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 python_smart_retry-0.1.0.tar.gz.
File metadata
- Download URL: python_smart_retry-0.1.0.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d3e19a0ff7adc0c263368116eb4982dac3aa28540d19a9ac637873d9352f1534
|
|
| MD5 |
3d9d6f33db0f10cb3fe27efa0dbb855d
|
|
| BLAKE2b-256 |
2ecebddccd10cfaf5ac42a4b516cdc077781203599981ab1380ce123bc622a70
|
File details
Details for the file python_smart_retry-0.1.0-py3-none-any.whl.
File metadata
- Download URL: python_smart_retry-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
42dc58d5f08896224e33138535d055387a60fd10fbd71a7dafe09e6c2ce181d1
|
|
| MD5 |
f1cddcb794a04aa1c713ac02e58a728b
|
|
| BLAKE2b-256 |
bd7bbfc8e628bbbb1fa104a0312098250495854fbce9afd11411ab0e0ed5cf09
|