Skip to main content

Async retry with exponential backoff, circuit breaker, and cancellation for Python

Project description

philiprehberger-retry-kit

Retry with exponential backoff, circuit breaker, and presets 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())

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

philiprehberger_retry_kit-0.1.1.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_retry_kit-0.1.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_retry_kit-0.1.1.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_retry_kit-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b144775aff8797a02bb992da9ae16dbc3a732d2c573e42f800a2c4e5d18df460
MD5 091328a294524692d0e0ee81401a125e
BLAKE2b-256 d7ad96188a749c19dd64608b04d71988f4f0c6c83299680100278244cb4a01c4

See more details on using hashes here.

File details

Details for the file philiprehberger_retry_kit-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_retry_kit-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a31050c4990ce7a410391318c5b73f7f12063b15f687d9fed027561a97bb1ff4
MD5 82ce7fb86d4adf9893d9025dd0f058cc
BLAKE2b-256 8bca528bc7cdf35c109be94c4fd80a157e6675ffd67739a7e94aff501725fa0b

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