Skip to main content

Tiny retry decorator for sync and async Python functions

Project description

retrymax

Tiny retry decorator for Python, works for sync and async functions, with optional verbose output and callback hooks.


Installation

pip install retrymax

Features

  • Retry synchronous or asynchronous functions
  • Optional exponential backoff
  • Optional verbose output to see retry attempts
  • Optional callback hook for custom actions on retry
  • Safe handling of exceptions, only retries specified exceptions

Usage

Synchronous example

from retrymax import retry

@retry(times=3, delay=1, verbose=True)
def fetch_data():
    print("Trying to fetch data...")
    raise ValueError("Failed!")

try:
    fetch_data()
except ValueError:
    print("All retries failed!")

Output:

Trying to fetch data...
[retrymax] Attempt 1 failed: Failed!
Trying to fetch data...
[retrymax] Attempt 2 failed: Failed!
Trying to fetch data...
[retrymax] Attempt 3 failed: Failed!
All retries failed!

Asynchronous example

import asyncio
from retrymax import retry

@retry(times=3, delay=0.5, verbose=True)
async def fetch_async():
    print("Trying async fetch...")
    raise ValueError("Async fail!")

async def main():
    try:
        await fetch_async()
    except ValueError:
        print("All async retries failed!")

asyncio.run(main())

Output:

Trying async fetch...
[retrymax] Attempt 1 failed: Async fail!
Trying async fetch...
[retrymax] Attempt 2 failed: Async fail!
Trying async fetch...
[retrymax] Attempt 3 failed: Async fail!
All async retries failed!

Using a callback hook

from retrymax import retry

def my_callback(exception, attempt):
    print(f"[callback] Retry {attempt} failed: {exception}")

@retry(times=3, delay=0.5, verbose=True, on_retry=my_callback)
def fetch_with_callback():
    print("Attempting fetch with callback...")
    raise ValueError("Failed again!")

try:
    fetch_with_callback()
except ValueError:
    print("All retries failed with callback!")

Output:

Attempting fetch with callback...
[retrymax] Attempt 1 failed: Failed again!
[callback] Retry 1 failed: Failed again!
Attempting fetch with callback...
[retrymax] Attempt 2 failed: Failed again!
[callback] Retry 2 failed: Failed again!
Attempting fetch with callback...
[retrymax] Attempt 3 failed: Failed again!
[callback] Retry 3 failed: Failed again!
All retries failed with callback!

Parameters

  • times (int): Number of retry attempts (default: 3)
  • delay (float): Delay between retries in seconds (default: 0)
  • backoff (str or None): "exp" for exponential backoff (default: None)
  • exceptions (tuple): Exceptions to catch and retry (default: (Exception,))
  • verbose (bool): Print retry attempts (default: False)
  • on_retry (callable): Optional callback called on each retry with (exception, attempt)

License

MIT License


Notes

  • Works with Python 3.8+
  • Supports both synchronous and asynchronous functions
  • Safe and consistent retry behavior
  • Perfect for network calls, flaky APIs, or any operation that may fail intermittently

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

retrymax-0.1.2.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

retrymax-0.1.2-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file retrymax-0.1.2.tar.gz.

File metadata

  • Download URL: retrymax-0.1.2.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for retrymax-0.1.2.tar.gz
Algorithm Hash digest
SHA256 b86b344d155c3541e94f4a93ce359c7eea9ffe6e421d6c2dbd3a9f34862880c4
MD5 0937534b3c23e5dfb8921ef17eb5a466
BLAKE2b-256 d7f761ab6ca05a3bf9b84e3b17ee906df16b60b20c305b1fa3925e9f6da8209a

See more details on using hashes here.

File details

Details for the file retrymax-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: retrymax-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for retrymax-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 913c99c113dc4b96bf1e70f276a2e76837e7668fe7f05ab872d4497e6e550423
MD5 f74928b5e1c53fa8881384565f0169e6
BLAKE2b-256 7aef8c08e7da3d2359aa3cfe76a27c8d1c5db78708d0a37893aa8eb11346c640

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