Skip to main content

A tool for exponential backoff that can be easily written in a loop form

Project description

※下の方に日本語の説明があります。

📖 iter-backoff Documentation

English Version

Overview

iter-backoff is a simple Python library that allows you to implement exponential backoff in loops with minimal effort. Instead of manually handling retry logic with sleep intervals, this library provides an elegant iterable interface to automate the process.

Installation

To install iter-backoff, use pip:

pip install iter-backoff

Usage

This library generates an iterable that yields exponential backoff times, allowing for simple loop-based retry logic.

Example:

import random
import iter_backoff

# Loop with exponential backoff
for _ in iter_backoff(s0=0.5, r=2, n=4):
    if random.random() < 1/4:
        print("Success!")
        break
    else:
        print("Failure!")

Explanation

  • The for loop automatically waits while repeating, with an exponentially increasing wait time.
  • If the process succeeds, simply breaking out of the loop is sufficient.
  • When n=1 is specified, it attempts only once without any waiting time.

Function Signature

iter_backoff(
    s0,  # Initial wait time (seconds)
    r,   # Growth rate (multiplier)
    n    # Maximum number of retries
)

Parameters

  • s0: The initial wait time (in seconds).
  • r: The exponential growth factor (e.g., 2 doubles the wait time each iteration).
  • n: The maximum number of retries.

日本語版

概要

iter-backoff は、指数バックオフをループで簡単に実装できる Python ライブラリです。

インストール

iter-backoff をインストールするには、以下のコマンドを実行してください:

pip install iter-backoff

使い方

このライブラリは、指数的に増加する待機時間を生成するイテレータを提供します。
このイテレータを for ループで利用することで、簡単にバックオフ付きのリトライ処理が書けます。

例:

import random
import iter_backoff

# ループによる指数バックオフ
for _ in iter_backoff(s0=0.5, r=2, n=4):
    if random.random() < 1/4:
        print("成功!")
        break
    else:
        print("失敗!")

説明

  • for文は、指数的に長くなる待機時間を自動的に待機しながら繰り返します。
  • 処理が成功した場合は単にbreakするだけでOKです。
  • n=1指定の場合、待ち時間なしで1回のみ試行されます。

関数の仕様

iter_backoff(
    s0,  # 初回待機時間 (秒)
    r,   # 待機時間の増加率 (倍率)
    n    # 最大試行回数
)

引数

  • s0: 初回の待機時間 (秒)。
  • r: 指数的な増加率 (例: 2 にすると毎回待機時間が 2 倍)。
  • n: 最大試行回数。

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

iter-backoff-0.0.0.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

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

iter_backoff-0.0.0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file iter-backoff-0.0.0.tar.gz.

File metadata

  • Download URL: iter-backoff-0.0.0.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.3

File hashes

Hashes for iter-backoff-0.0.0.tar.gz
Algorithm Hash digest
SHA256 6de72772855d81ee17ca01dfe3d9d0f743ca1ecfa65da8c8152fed9d6ab4421d
MD5 6149ffb804ff781f5270347e3ee6f1b4
BLAKE2b-256 fe7ca487465ec13cf02ffe17cd0d36b1bec962b235812619f8f34f1487640027

See more details on using hashes here.

File details

Details for the file iter_backoff-0.0.0-py3-none-any.whl.

File metadata

  • Download URL: iter_backoff-0.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.3

File hashes

Hashes for iter_backoff-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e82e06c18c7fad1f13f523d7841f5210e0b3f192d81c185a53b3e490da8f0cd
MD5 b057a07a4b7165c91ebcc2b40a4f16b7
BLAKE2b-256 13e0f6ccd48a477e1abd566962a7c829c06e0c7cc0fa38717717513da30c90e4

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