Skip to main content

A Python decorator for retrying functions with exponential backoff, optional jitter, and support for both sync and async code.

Reason this release was yanked:

typing issues

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, 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.8+.
  • Works transparently for both sync and async code.

🛠️ License

MIT License

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

retrytools-0.0.3.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

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

retrytools-0.0.3-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file retrytools-0.0.3.tar.gz.

File metadata

  • Download URL: retrytools-0.0.3.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.8

File hashes

Hashes for retrytools-0.0.3.tar.gz
Algorithm Hash digest
SHA256 62a1946fc2526729875d970d612da34f3aaa5fc5b766109f982c7cc6ba046b08
MD5 0357dc7f497c1dfd100814b4baf32259
BLAKE2b-256 8c0bd6088bf0d9d3f4d38489cf442b0c3098d0f0c650b019c68d9fd14acdbe59

See more details on using hashes here.

File details

Details for the file retrytools-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for retrytools-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2f1ab1e7289827dab581309ecc0cfbd79bf39c3c896343ef5f30914579ca0086
MD5 e9d19bad99f39b8d2d0274e1bee70cba
BLAKE2b-256 3120ad20d59b65e6cd129bec863373a9cf56c68282b325f2ad27b9f3d3de7deb

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