Skip to main content

Function decoration for backoff and retry

Project description

backon

Function decoration for backoff and retry — modern, fast, and zero dependencies.

CI PyPI Python License

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 value
  • random_jitter — random ±25% around the wait value
  • None — 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

backon-3.1.0.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

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

backon-3.1.0-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file backon-3.1.0.tar.gz.

File metadata

  • Download URL: backon-3.1.0.tar.gz
  • Upload date:
  • Size: 16.4 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

Hashes for backon-3.1.0.tar.gz
Algorithm Hash digest
SHA256 53dc9b3382159e8235066695859cb456798c2f54b5e04d75cbb7052a04b0f87d
MD5 4e1225cfe821b46a8aa24f743a41f2aa
BLAKE2b-256 5f539a0c2c3fa5185fa4a7ce7297443bd803dfa34c4e5546779b02729caf2c89

See more details on using hashes here.

File details

Details for the file backon-3.1.0-py3-none-any.whl.

File metadata

  • Download URL: backon-3.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.8 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

Hashes for backon-3.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ed3914f52a0802af82f02a589974ae4555e04ae76a29c5a402f584a90ba29a31
MD5 c1a26f3ba9cfaeea8585bfc8dde3a5c9
BLAKE2b-256 fff00fee586e40f97be480d4c2b3fa4d9454c7610b63537f7a31a87eb21111bf

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