Skip to main content

A flexible Python decorator for retrying functions upon failure with support for exponential backoff, jitter, and configurable retry policies.

Project description

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

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

refire-0.1.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

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

refire-0.1.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file refire-0.1.0.tar.gz.

File metadata

  • Download URL: refire-0.1.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.13

File hashes

Hashes for refire-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a488b90b161330f41244cddf678a9b88a3f4caf0bab7aa95f7c3e9da70bb9b39
MD5 cc99f2e79499d66a3429316b779fd74c
BLAKE2b-256 59283c73a275e3620058a98e651ab4360e4378e25486583dfcbe2a5d73d9e975

See more details on using hashes here.

File details

Details for the file refire-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: refire-0.1.0-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

Hashes for refire-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d1abc656449c6042b9489ec7c0218886aab4617a6fbcad710b82fa6e33acea6e
MD5 85d3f71a114aa890acf5542f392fcde4
BLAKE2b-256 5eda110e5a702c8ec07647b2b730fd24142a730ef2ed21f503392af5df64d798

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