refire
A flexible Python decorator for retrying functions upon failure with support for exponential backoff, jitter, and configurable retry policies.
Features
- Retry functions automatically when exceptions occur.
- Configurable number of retries or infinite retries.
- Adjustable initial delay and exponential backoff.
- Optional maximum delay to cap wait times.
- Support for random jitter to prevent retry storms (thundering herd problem).
- Specify which exception types trigger retries.
Installation
Install via pip:
pip install refire
# or
uv add refire
Or clone the repository:
git clone https://github.com/maxscheijen/refire.git
cd refire
pip install .
# or
uv sync
Usage
Basic Example
import random
from refire import refire
@refire(tries=5, delay=2, backoff=2, jitter=(0, 1))
def flaky_function():
if random.random() < 0.7:
raise ValueError("Unlucky!")
return "Success!"
result = flaky_function()
print(result) # "Success!" after several retries
Custom Exception
class CustomError(Exception):
pass
@refire(exceptions=CustomError, tries=3, delay=1)
def risky_function():
raise CustomError("Oops!")
risky_function()
Logging
Retries are by default logged at WARNING level:
Caught ValueError: Unlucky!. Retrying in 2.00s (remaining=4)
Development
This project uses uv. To set up a local development environment:
# Clone the repository
git clone https://github.com/maxscheijen/refire.git
cd refire
# Install dependencies (using uv)
uv sync --extra dev
# Or using pip
pip install -e ".[dev]"
Tests
This project uses pytest:
pytest
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
refire-0.1.1.tar.gz
(3.8 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 refire-0.1.1.tar.gz.
File metadata
- Download URL: refire-0.1.1.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.8.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cfce163e4a9997797bf4d2282d22aef275512cce23b9ba2181857ae4e788cffa
|
|
| MD5 |
c69f46884acf2c32a435fafc446fe47d
|
|
| BLAKE2b-256 |
2897b36b52ef25203d785c0e2234318a9122a29ccb51ffe2b263c1aa6888db46
|
File details
Details for the file refire-0.1.1-py3-none-any.whl.
File metadata
- Download URL: refire-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.8.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d5510c32373ebb1902c0af7822cf6ac3fde4f25c188624a4d426a2fd349aa235
|
|
| MD5 |
5e34bb01037cb4b158412f60b16ff6d1
|
|
| BLAKE2b-256 |
3b4ad57463d5b1ffe378b5d5a3555d1963f8bc0eb0f2a607285b24282d84a27a
|