Skip to main content

A universal smart retry library for Python (sync + async)

Project description

🚀 Smart Retry

PyPI version Python versions License: MIT

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?

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 def and async 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, and on_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.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. 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


Download files

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

Source Distribution

python_smart_retry-0.1.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

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

python_smart_retry-0.1.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

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

Hashes for python_smart_retry-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d3e19a0ff7adc0c263368116eb4982dac3aa28540d19a9ac637873d9352f1534
MD5 3d9d6f33db0f10cb3fe27efa0dbb855d
BLAKE2b-256 2ecebddccd10cfaf5ac42a4b516cdc077781203599981ab1380ce123bc622a70

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_smart_retry-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 42dc58d5f08896224e33138535d055387a60fd10fbd71a7dafe09e6c2ce181d1
MD5 f1cddcb794a04aa1c713ac02e58a728b
BLAKE2b-256 bd7bbfc8e628bbbb1fa104a0312098250495854fbce9afd11411ab0e0ed5cf09

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