Function decoration for backoff and retry
Project description
backon
Function decoration for backoff and retry — modern, fast, and zero dependencies.
Why backon?
backon is the evolution of backoff — a zero-dependency Python library for retry with exponential backoff. If you know backoff, you already know backon.
| Feature | backoff | tenacity | backon |
|---|---|---|---|
| Python 3.10+ native | ❌ | ❌ | ✅ |
| Type hints | ❌ partial | ✅ | ✅ full |
disable() / enable() toggle |
❌ | ❌ | ✅ |
| Context manager API | ❌ | ✅ | ✅ |
Functional retry() API |
❌ | ✅ | ✅ |
on_attempt callback |
❌ | ✅ | ✅ |
| Custom sleep injection | ❌ | ❌ | ✅ |
time.monotonic() |
❌ | ✅ | ✅ |
| PDM / PEP 621 build | ❌ | ❌ | ✅ |
| Zero dependencies | ✅ | ✅ | ✅ |
Quick Start
pip install backon
Retry on exception
import backon
@backon.on_exception(backon.expo, ValueError, max_tries=3)
def fetch_data():
return api.call()
Retry on predicate
@backon.on_predicate(backon.constant, max_tries=5, interval=0.5)
def poll_status():
return check_ready()
Functional API
result = backon.retry(fetch_data, backon.expo, exception=ValueError, max_tries=3)
Context manager
with backon.Retrying(backon.expo, exception=ValueError, max_tries=3) as r:
result = r.call(fetch_data)
Wait Generators
| Generator | Description |
|---|---|
expo(base=2, factor=1, max_value=None) |
Exponential backoff |
constant(interval=1) |
Constant interval |
fibo(max_value=None) |
Fibonacci backoff |
runtime(value=callable) |
Dynamic wait from return value |
decay(initial_value=1, decay_factor=1, min_value=None) |
Exponential decay |
Jitter
@backon.on_exception(backon.expo, ValueError, jitter=backon.full_jitter)
full_jitter— random between 0 and the wait valuerandom_jitter— random ±25% around the wait valueNone— no jitter
Handlers
def log_attempt(details):
print(f"Attempt {details['tries']} for {details['target'].__name__}")
@backon.on_exception(
backon.expo, ValueError, max_tries=3,
on_attempt=log_attempt,
on_backoff=log_attempt,
on_success=log_attempt,
on_giveup=log_attempt,
)
def f():
...
Available details keys: target, args, kwargs, tries, elapsed, value (on_success/on_backoff/on_giveup), exception (on_backoff/on_giveup), wait (on_backoff).
Global Toggle
backon.disable() # skip retry, call function directly
backon.enable() # re-enable retry
Async Support
Everything works with async def functions — no extra flags needed.
@backon.on_exception(backon.expo, ValueError, max_tries=3)
async def fetch_data():
return await api.call()
Custom Sleep
@backon.on_exception(backon.expo, ValueError, max_tries=3,
sleep=lambda s: print(f"waiting {s}s"))
def f():
...
Installation
pip install backon
Requires Python 3.10+.
Migrating from backoff
backon is a drop-in replacement for most backoff users. Just change:
# before
import backoff
@backoff.on_exception(backoff.expo, ValueError, max_tries=3)
# after
import backon
@backon.on_exception(backon.expo, ValueError, max_tries=3)
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 backon-3.0.0.tar.gz.
File metadata
- Download URL: backon-3.0.0.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.28.0 CPython/3.12.13 Linux/6.17.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0cd1f530c165617804d9712720c5eeb81949517b4ca22eae08e4b8f18d2bbb69
|
|
| MD5 |
1d3c1b27f3efc5a122282347f2ec7003
|
|
| BLAKE2b-256 |
a755cc093d8a7fca97fa49acabf59aeff9e5f57f444b1c5d862e21f7822b13e1
|
File details
Details for the file backon-3.0.0-py3-none-any.whl.
File metadata
- Download URL: backon-3.0.0-py3-none-any.whl
- Upload date:
- Size: 12.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.28.0 CPython/3.12.13 Linux/6.17.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
615055d9f5e08c759261b69487a9075df23493659db424e4ae698cc1929c025f
|
|
| MD5 |
f855b6a6dfef175c1850a541db697313
|
|
| BLAKE2b-256 |
2189bdf4558034d35f4b3b33986c9ddbade4bfcf250d386d837299b931ee57c6
|