Async retry with exponential backoff, circuit breaker, and cancellation for Python
Project description
philiprehberger-retry-kit
Async retry with exponential backoff, circuit breaker, and cancellation for Python.
Installation
pip install philiprehberger-retry-kit
Usage
Basic Retry
from philiprehberger_retry_kit import retry
result = retry(lambda: fetch_data(), max_attempts=3)
With Options
result = retry(
lambda: fetch_data(),
max_attempts=5,
backoff="exponential", # "exponential" | "linear" | "fixed"
initial_delay=1.0,
max_delay=30.0,
jitter=True,
retry_on=lambda e: isinstance(e, ConnectionError),
on_retry=lambda e, attempt: print(f"Retry {attempt}..."),
)
Async Retry
from philiprehberger_retry_kit import async_retry
result = await async_retry(
lambda: async_fetch_data(),
max_attempts=3,
backoff="exponential",
)
Presets
from philiprehberger_retry_kit import retry, presets
result = retry(lambda: fetch_data(), **presets["network_request"])
result = retry(lambda: db_query(), **presets["database_query"])
result = retry(lambda: critical_op(), **presets["aggressive"])
result = retry(lambda: gentle_op(), **presets["gentle"])
Circuit Breaker
from philiprehberger_retry_kit import CircuitBreaker, CircuitOpenError
breaker = CircuitBreaker(
failure_threshold=5,
reset_timeout=30.0,
on_state_change=lambda from_s, to_s: print(f"Circuit: {from_s} -> {to_s}"),
)
try:
result = breaker.call(lambda: fetch_data())
except CircuitOpenError:
print("Circuit is open, failing fast")
Async Circuit Breaker
result = await breaker.async_call(lambda: async_fetch_data())
API
| Function / Class | Description |
|---|---|
retry(fn, *, max_attempts=3, backoff="exponential", initial_delay=1.0, max_delay=30.0, jitter=True, retry_on=None, on_retry=None, on_success=None, on_failure=None) |
Retry a callable with configurable backoff |
async_retry(fn, *, ...) |
Async version of retry() with the same parameters |
CircuitBreaker(failure_threshold=5, reset_timeout=30.0, half_open_max_attempts=1, on_state_change=None, on_circuit_open=None) |
Circuit breaker that fails fast after repeated failures |
CircuitBreaker.call(fn) |
Execute function through the circuit breaker |
CircuitBreaker.async_call(fn) |
Async version of call() |
RetryError |
Raised when all retry attempts fail (.attempts, .last_error) |
CircuitOpenError |
Raised when circuit breaker is open |
presets |
Dict of preset configs: "aggressive", "gentle", "network_request", "database_query" |
Development
pip install -e .
python -m pytest tests/ -v
License
MIT
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
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 philiprehberger_retry_kit-0.2.5.tar.gz.
File metadata
- Download URL: philiprehberger_retry_kit-0.2.5.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4e71d5803ba2eb0344386b71b95798bc994f344ff89da910f07cc59c5bb1d4e
|
|
| MD5 |
1e7dd7df0fa6150f4706cf1d17d416f6
|
|
| BLAKE2b-256 |
0d20a08bc6cb8393a54b9d34ffba2da911fb5b5afab54276b6c5c4584d5a252a
|
File details
Details for the file philiprehberger_retry_kit-0.2.5-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_retry_kit-0.2.5-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f2b5b7223825157e02a98d1e7167ee1bd785062f90993f59f0e6862f1cc3c6f3
|
|
| MD5 |
6f4e0bdd37a4b4ee38fbfa9fc9b26bb5
|
|
| BLAKE2b-256 |
162d1a1cdcc0b05d2e950c03d9eebc4c374229a8a7435b601cad9227dbe001da
|