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
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
retrymax-0.1.1.tar.gz
(3.9 kB
view details)
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 retrymax-0.1.1.tar.gz.
File metadata
- Download URL: retrymax-0.1.1.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c4e7fdb41b78f62f68b38f42dc000d73ae28913f746a020a1c3cf5006d7692cb
|
|
| MD5 |
dba0390016d4b1042a0ccc211c48c39f
|
|
| BLAKE2b-256 |
0bf5400ca763cab0a01c031c301cbf7b7477f3c7616b8f7bef8c42f2b8e73e3b
|
File details
Details for the file retrymax-0.1.1-py3-none-any.whl.
File metadata
- Download URL: retrymax-0.1.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f87ed79c8f559e9ff56f5d82c2dcc6d3073d6242c35b4275ef6bc07a124708a1
|
|
| MD5 |
e5d8f4b50c968873a775458ca8f1cf13
|
|
| BLAKE2b-256 |
024bf2cd649e056f5b4d76fa2178f4c954c5e079736952ff0cf547ed9db43f51
|