🚀 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
Metadata
Release files for python-smart-retry 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| python_smart_retry-0.1.0.tar.gz | 8.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_smart_retry-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.6 kB
Release files / python_smart_retry-0.1.0.tar.gz
| Download URL | python_smart_retry-0.1.0.tar.gz |
|---|---|
| Size | 8.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d3e19a0ff7adc0c263368116eb4982dac3aa28540d19a9ac637873d9352f1534
|
|
BLAKE2b-256 checksum How to use checksums |
2ecebddccd10cfaf5ac42a4b516cdc077781203599981ab1380ce123bc622a70
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.7
|
Release files / python_smart_retry-0.1.0-py3-none-any.whl
| Download URL | python_smart_retry-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
42dc58d5f08896224e33138535d055387a60fd10fbd71a7dafe09e6c2ce181d1
|
|
BLAKE2b-256 checksum How to use checksums |
bd7bbfc8e628bbbb1fa104a0312098250495854fbce9afd11411ab0e0ed5cf09
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.7
|