Skip to content

API Reference

Complete API documentation for the backoff module.

Decorators

backoff.on_exception

on_exception(wait_gen, exception, *, max_tries=None, max_time=None, jitter=full_jitter, giveup=lambda e: False, on_success=None, on_backoff=None, on_giveup=None, raise_on_giveup=True, logger='backoff', backoff_log_level=logging.INFO, giveup_log_level=logging.ERROR, **wait_gen_kwargs)

Returns decorator for backoff and retry triggered by exception.

Parameters:

Name Type Description Default
wait_gen _WaitGenerator

A generator yielding successive wait times in seconds.

required
exception _MaybeTuple[type[Exception]]

An exception type (or tuple of types) which triggers backoff.

required
max_tries _MaybeCallable[int] | None

The maximum number of attempts to make before giving up. Once exhausted, the exception will be allowed to escape. The default value of None means there is no limit to the number of tries. If a callable is passed, it will be evaluated at runtime and its return value used.

None
max_time _MaybeCallable[float] | None

The maximum total amount of time to try for before giving up. Once expired, the exception will be allowed to escape. If a callable is passed, it will be evaluated at runtime and its return value used.

None
jitter _Jitterer | None

A function of the value yielded by wait_gen returning the actual time to wait. This distributes wait times stochastically in order to avoid timing collisions across concurrent clients. Wait times are jittered by default using the full_jitter function. Jittering may be disabled altogether by passing jitter=None.

full_jitter
giveup _Predicate[Exception]

Function accepting an exception instance and returning whether or not to give up. Optional. The default is to always continue.

lambda e: False
on_success _Handler | Iterable[_Handler] | None

Callable (or iterable of callables) with a unary signature to be called in the event of success. The parameter is a dict containing details about the invocation.

None
on_backoff _Handler | Iterable[_Handler] | None

Callable (or iterable of callables) with a unary signature to be called in the event of a backoff. The parameter is a dict containing details about the invocation.

None
on_giveup _Handler | Iterable[_Handler] | None

Callable (or iterable of callables) with a unary signature to be called in the event that max_tries is exceeded. The parameter is a dict containing details about the invocation.

None
raise_on_giveup bool

Boolean indicating whether the registered exceptions should be raised on giveup. Defaults to True

True
logger _MaybeLogger

Name or Logger object to log to. Defaults to 'backoff'.

'backoff'
backoff_log_level int

log level for the backoff event. Defaults to "INFO"

INFO
giveup_log_level int

log level for the give up event. Defaults to "ERROR"

ERROR
**wait_gen_kwargs Any

Any additional keyword args specified will be passed to wait_gen when it is initialized. Any callable args will first be evaluated and their return values passed. This is useful for runtime configuration.

{}
Source code in backoff/_decorator.py
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
def on_exception(
    wait_gen: _WaitGenerator,
    exception: _MaybeTuple[type[Exception]],
    *,
    max_tries: _MaybeCallable[int] | None = None,
    max_time: _MaybeCallable[float] | None = None,
    jitter: _Jitterer | None = full_jitter,
    giveup: _Predicate[Exception] = lambda e: False,
    on_success: _Handler | Iterable[_Handler] | None = None,
    on_backoff: _Handler | Iterable[_Handler] | None = None,
    on_giveup: _Handler | Iterable[_Handler] | None = None,
    raise_on_giveup: bool = True,
    logger: _MaybeLogger = "backoff",
    backoff_log_level: int = logging.INFO,
    giveup_log_level: int = logging.ERROR,
    **wait_gen_kwargs: Any,
) -> Callable[[Callable[P, T]], Callable[P, T]]:
    """Returns decorator for backoff and retry triggered by exception.

    Args:
        wait_gen: A generator yielding successive wait times in
            seconds.
        exception: An exception type (or tuple of types) which triggers
            backoff.
        max_tries: The maximum number of attempts to make before giving
            up. Once exhausted, the exception will be allowed to escape.
            The default value of None means there is no limit to the
            number of tries. If a callable is passed, it will be
            evaluated at runtime and its return value used.
        max_time: The maximum total amount of time to try for before
            giving up. Once expired, the exception will be allowed to
            escape. If a callable is passed, it will be
            evaluated at runtime and its return value used.
        jitter: A function of the value yielded by wait_gen returning
            the actual time to wait. This distributes wait times
            stochastically in order to avoid timing collisions across
            concurrent clients. Wait times are jittered by default
            using the full_jitter function. Jittering may be disabled
            altogether by passing jitter=None.
        giveup: Function accepting an exception instance and
            returning whether or not to give up. Optional. The default
            is to always continue.
        on_success: Callable (or iterable of callables) with a unary
            signature to be called in the event of success. The
            parameter is a dict containing details about the invocation.
        on_backoff: Callable (or iterable of callables) with a unary
            signature to be called in the event of a backoff. The
            parameter is a dict containing details about the invocation.
        on_giveup: Callable (or iterable of callables) with a unary
            signature to be called in the event that max_tries
            is exceeded.  The parameter is a dict containing details
            about the invocation.
        raise_on_giveup: Boolean indicating whether the registered exceptions
            should be raised on giveup. Defaults to `True`
        logger: Name or Logger object to log to. Defaults to 'backoff'.
        backoff_log_level: log level for the backoff event. Defaults to "INFO"
        giveup_log_level: log level for the give up event. Defaults to "ERROR"
        **wait_gen_kwargs: Any additional keyword args specified will be
            passed to wait_gen when it is initialized.  Any callable
            args will first be evaluated and their return values passed.
            This is useful for runtime configuration.
    """

    def decorate(target: Callable[P, T]) -> Callable[P, T]:
        nonlocal logger, on_success, on_backoff, on_giveup

        logger = _prepare_logger(logger)
        on_success = _config_handlers(on_success)
        on_backoff = _config_handlers(
            on_backoff,
            default_handler=_log_backoff,
            logger=logger,
            log_level=backoff_log_level,
        )
        on_giveup = _config_handlers(
            on_giveup,
            default_handler=_log_giveup,
            logger=logger,
            log_level=giveup_log_level,
        )

        if inspect.iscoroutinefunction(target):
            retry = _async.retry_exception
        else:
            retry = _sync.retry_exception

        return retry(
            target,
            wait_gen,
            exception,
            max_tries=max_tries,
            max_time=max_time,
            jitter=jitter,
            giveup=giveup,
            on_success=on_success,
            on_backoff=on_backoff,
            on_giveup=on_giveup,
            raise_on_giveup=raise_on_giveup,
            wait_gen_kwargs=wait_gen_kwargs,
        )

    # Return a function which decorates a target with a retry loop.
    return decorate

backoff.on_predicate

on_predicate(wait_gen, predicate=operator.not_, *, max_tries=None, max_time=None, jitter=full_jitter, on_success=None, on_backoff=None, on_giveup=None, logger='backoff', backoff_log_level=logging.INFO, giveup_log_level=logging.ERROR, **wait_gen_kwargs)

Returns decorator for backoff and retry triggered by predicate.

Parameters:

Name Type Description Default
wait_gen _WaitGenerator

A generator yielding successive wait times in seconds.

required
predicate _Predicate[Any]

A function which when called on the return value of the target function will trigger backoff when considered truthily. If not specified, the default behavior is to backoff on falsey return values.

not_
max_tries _MaybeCallable[int] | None

The maximum number of attempts to make before giving up. In the case of failure, the result of the last attempt will be returned. The default value of None means there is no limit to the number of tries. If a callable is passed, it will be evaluated at runtime and its return value used.

None
max_time _MaybeCallable[float] | None

The maximum total amount of time in seconds to try for before giving up. If this time expires, the result of the last attempt will be returned. If a callable is passed, it will be evaluated at runtime and its return value used.

None
jitter _Jitterer | None

A function of the value yielded by wait_gen returning the actual time to wait. This distributes wait times stochastically in order to avoid timing collisions across concurrent clients. Wait times are jittered by default using the full_jitter function. Jittering may be disabled altogether by passing jitter=None.

full_jitter
on_success _Handler | Iterable[_Handler] | None

Callable (or iterable of callables) with a unary signature to be called in the event of success. The parameter is a dict containing details about the invocation.

None
on_backoff _Handler | Iterable[_Handler] | None

Callable (or iterable of callables) with a unary signature to be called in the event of a backoff. The parameter is a dict containing details about the invocation.

None
on_giveup _Handler | Iterable[_Handler] | None

Callable (or iterable of callables) with a unary signature to be called in the event that max_tries is exceeded. The parameter is a dict containing details about the invocation.

None
logger _MaybeLogger

Name of logger or Logger object to log to. Defaults to 'backoff'.

'backoff'
backoff_log_level int

log level for the backoff event. Defaults to "INFO"

INFO
giveup_log_level int

log level for the give up event. Defaults to "ERROR"

ERROR
**wait_gen_kwargs Any

Any additional keyword args specified will be passed to wait_gen when it is initialized. Any callable args will first be evaluated and their return values passed. This is useful for runtime configuration.

{}
Source code in backoff/_decorator.py
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def on_predicate(
    wait_gen: _WaitGenerator,
    predicate: _Predicate[Any] = operator.not_,
    *,
    max_tries: _MaybeCallable[int] | None = None,
    max_time: _MaybeCallable[float] | None = None,
    jitter: _Jitterer | None = full_jitter,
    on_success: _Handler | Iterable[_Handler] | None = None,
    on_backoff: _Handler | Iterable[_Handler] | None = None,
    on_giveup: _Handler | Iterable[_Handler] | None = None,
    logger: _MaybeLogger = "backoff",
    backoff_log_level: int = logging.INFO,
    giveup_log_level: int = logging.ERROR,
    **wait_gen_kwargs: Any,
) -> Callable[[Callable[P, T]], Callable[P, T]]:
    """Returns decorator for backoff and retry triggered by predicate.

    Args:
        wait_gen: A generator yielding successive wait times in
            seconds.
        predicate: A function which when called on the return value of
            the target function will trigger backoff when considered
            truthily. If not specified, the default behavior is to
            backoff on falsey return values.
        max_tries: The maximum number of attempts to make before giving
            up. In the case of failure, the result of the last attempt
            will be returned. The default value of None means there
            is no limit to the number of tries. If a callable is passed,
            it will be evaluated at runtime and its return value used.
        max_time: The maximum total amount of time in seconds to try for before
            giving up. If this time expires, the result of the last
            attempt will be returned. If a callable is passed, it will
            be evaluated at runtime and its return value used.
        jitter: A function of the value yielded by wait_gen returning
            the actual time to wait. This distributes wait times
            stochastically in order to avoid timing collisions across
            concurrent clients. Wait times are jittered by default
            using the full_jitter function. Jittering may be disabled
            altogether by passing jitter=None.
        on_success: Callable (or iterable of callables) with a unary
            signature to be called in the event of success. The
            parameter is a dict containing details about the invocation.
        on_backoff: Callable (or iterable of callables) with a unary
            signature to be called in the event of a backoff. The
            parameter is a dict containing details about the invocation.
        on_giveup: Callable (or iterable of callables) with a unary
            signature to be called in the event that max_tries
            is exceeded.  The parameter is a dict containing details
            about the invocation.
        logger: Name of logger or Logger object to log to. Defaults to
            'backoff'.
        backoff_log_level: log level for the backoff event. Defaults to "INFO"
        giveup_log_level: log level for the give up event. Defaults to "ERROR"
        **wait_gen_kwargs: Any additional keyword args specified will be
            passed to wait_gen when it is initialized.  Any callable
            args will first be evaluated and their return values passed.
            This is useful for runtime configuration.
    """

    def decorate(target: Callable[P, T]) -> Callable[P, T]:
        nonlocal logger, on_success, on_backoff, on_giveup

        logger = _prepare_logger(logger)
        on_success = _config_handlers(on_success)
        on_backoff = _config_handlers(
            on_backoff,
            default_handler=_log_backoff,
            logger=logger,
            log_level=backoff_log_level,
        )
        on_giveup = _config_handlers(
            on_giveup,
            default_handler=_log_giveup,
            logger=logger,
            log_level=giveup_log_level,
        )

        if inspect.iscoroutinefunction(target):
            retry = _async.retry_predicate
        else:
            retry = _sync.retry_predicate

        return retry(
            target,
            wait_gen,
            predicate,
            max_tries=max_tries,
            max_time=max_time,
            jitter=jitter,
            on_success=on_success,
            on_backoff=on_backoff,
            on_giveup=on_giveup,
            wait_gen_kwargs=wait_gen_kwargs,
        )

    # Return a function which decorates a target with a retry loop.
    return decorate

Context Managers

backoff.retry_context

retry_context(exception=Exception, wait_gen=expo, *, max_tries=None, max_time=None, jitter=full_jitter, giveup=lambda e: False, on_success=None, on_backoff=None, on_giveup=None, raise_on_giveup=True, logger='backoff', backoff_log_level=logging.INFO, giveup_log_level=logging.ERROR, **wait_gen_kwargs)

Returns a generator of retry attempts, for direct use with a for loop.

Unlike on_exception, this doesn't wrap a whole function; it lets a caller retry an arbitrary block of code:

for attempt in backoff.retry_context(ValueError, backoff.expo):
    with attempt:
        do_something()

Each attempt is a context manager: exceptions matching exception are caught, and the loop either sleeps and retries or lets the exception (or a different one) propagate once retries are exhausted. Succeeding (no exception raised in the with block) ends the loop.

Parameters:

Name Type Description Default
exception _MaybeTuple[type[Exception]]

An exception type (or tuple of types) which triggers backoff.

Exception
wait_gen _WaitGenerator

A generator yielding successive wait times in seconds.

expo
max_tries _MaybeCallable[int] | None

The maximum number of attempts to make before giving up. Once exhausted, the exception will be allowed to escape. The default value of None means there is no limit to the number of tries. If a callable is passed, it will be evaluated at runtime and its return value used.

None
max_time _MaybeCallable[float] | None

The maximum total amount of time to try for before giving up. Once expired, the exception will be allowed to escape. If a callable is passed, it will be evaluated at runtime and its return value used.

None
jitter _Jitterer | None

A function of the value yielded by wait_gen returning the actual time to wait. Jittered by default using full_jitter; disable with jitter=None.

full_jitter
giveup _Predicate[BaseException]

Function accepting an exception instance and returning whether or not to give up. Optional. The default is to always continue.

lambda e: False
on_success _ContextHandler | Iterable[_ContextHandler] | None

Callable (or iterable of callables) with a unary signature called on success. The parameter is a dict with tries and elapsed.

None
on_backoff _ContextHandler | Iterable[_ContextHandler] | None

Callable (or iterable of callables) called on backoff. The parameter dict additionally has wait and exception.

None
on_giveup _ContextHandler | Iterable[_ContextHandler] | None

Callable (or iterable of callables) called when giving up. The parameter dict additionally has exception.

None
raise_on_giveup bool

Boolean indicating whether the registered exception should be raised on giveup. Defaults to True.

True
logger _MaybeLogger

Name or Logger object to log to. Defaults to 'backoff'.

'backoff'
backoff_log_level int

log level for the backoff event. Defaults to "INFO"

INFO
giveup_log_level int

log level for the give up event. Defaults to "ERROR"

ERROR
**wait_gen_kwargs Any

Any additional keyword args specified will be passed to wait_gen when it is initialized.

{}
Source code in backoff/_decorator.py
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
def retry_context(
    exception: _MaybeTuple[type[Exception]] = Exception,
    wait_gen: _WaitGenerator = expo,
    *,
    max_tries: _MaybeCallable[int] | None = None,
    max_time: _MaybeCallable[float] | None = None,
    jitter: _Jitterer | None = full_jitter,
    giveup: _Predicate[BaseException] = lambda e: False,
    on_success: _ContextHandler | Iterable[_ContextHandler] | None = None,
    on_backoff: _ContextHandler | Iterable[_ContextHandler] | None = None,
    on_giveup: _ContextHandler | Iterable[_ContextHandler] | None = None,
    raise_on_giveup: bool = True,
    logger: _MaybeLogger = "backoff",
    backoff_log_level: int = logging.INFO,
    giveup_log_level: int = logging.ERROR,
    **wait_gen_kwargs: Any,
) -> Generator[_Attempt, None, None]:
    """Returns a generator of retry attempts, for direct use with a `for` loop.

    Unlike `on_exception`, this doesn't wrap a whole function; it lets a
    caller retry an arbitrary block of code:

        for attempt in backoff.retry_context(ValueError, backoff.expo):
            with attempt:
                do_something()

    Each `attempt` is a context manager: exceptions matching `exception`
    are caught, and the loop either sleeps and retries or lets the
    exception (or a different one) propagate once retries are exhausted.
    Succeeding (no exception raised in the `with` block) ends the loop.

    Args:
        exception: An exception type (or tuple of types) which triggers
            backoff.
        wait_gen: A generator yielding successive wait times in seconds.
        max_tries: The maximum number of attempts to make before giving
            up. Once exhausted, the exception will be allowed to escape.
            The default value of None means there is no limit to the
            number of tries. If a callable is passed, it will be
            evaluated at runtime and its return value used.
        max_time: The maximum total amount of time to try for before
            giving up. Once expired, the exception will be allowed to
            escape. If a callable is passed, it will be evaluated at
            runtime and its return value used.
        jitter: A function of the value yielded by wait_gen returning
            the actual time to wait. Jittered by default using
            full_jitter; disable with jitter=None.
        giveup: Function accepting an exception instance and returning
            whether or not to give up. Optional. The default is to
            always continue.
        on_success: Callable (or iterable of callables) with a unary
            signature called on success. The parameter is a dict with
            `tries` and `elapsed`.
        on_backoff: Callable (or iterable of callables) called on
            backoff. The parameter dict additionally has `wait` and
            `exception`.
        on_giveup: Callable (or iterable of callables) called when
            giving up. The parameter dict additionally has `exception`.
        raise_on_giveup: Boolean indicating whether the registered
            exception should be raised on giveup. Defaults to `True`.
        logger: Name or Logger object to log to. Defaults to 'backoff'.
        backoff_log_level: log level for the backoff event. Defaults to "INFO"
        giveup_log_level: log level for the give up event. Defaults to "ERROR"
        **wait_gen_kwargs: Any additional keyword args specified will be
            passed to wait_gen when it is initialized.
    """
    logger = _prepare_logger(logger)
    on_success = _config_handlers(on_success)
    on_backoff = _config_handlers(
        on_backoff,
        default_handler=_log_backoff_context,
        logger=logger,
        log_level=backoff_log_level,
    )
    on_giveup = _config_handlers(
        on_giveup,
        default_handler=_log_giveup_context,
        logger=logger,
        log_level=giveup_log_level,
    )

    return _sync.retry_context(
        exception,
        wait_gen,
        max_tries=max_tries,
        max_time=max_time,
        jitter=jitter,
        giveup=giveup,
        on_success=on_success,
        on_backoff=on_backoff,
        on_giveup=on_giveup,
        raise_on_giveup=raise_on_giveup,
        wait_gen_kwargs=wait_gen_kwargs,
    )

backoff.aretry_context

aretry_context(exception=Exception, wait_gen=expo, *, max_tries=None, max_time=None, jitter=full_jitter, giveup=lambda e: False, on_success=None, on_backoff=None, on_giveup=None, raise_on_giveup=True, logger='backoff', backoff_log_level=logging.INFO, giveup_log_level=logging.ERROR, **wait_gen_kwargs)

Async counterpart to retry_context, for use with async for.

async for attempt in backoff.aretry_context(ValueError, backoff.expo):
    with attempt:
        await do_something()

giveup and the handlers may be sync or async callables. See retry_context for the full argument reference.

Source code in backoff/_decorator.py
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
def aretry_context(
    exception: _MaybeTuple[type[Exception]] = Exception,
    wait_gen: _WaitGenerator = expo,
    *,
    max_tries: _MaybeCallable[int] | None = None,
    max_time: _MaybeCallable[float] | None = None,
    jitter: _Jitterer | None = full_jitter,
    giveup: _Predicate[BaseException] = lambda e: False,
    on_success: _ContextHandler | Iterable[_ContextHandler] | None = None,
    on_backoff: _ContextHandler | Iterable[_ContextHandler] | None = None,
    on_giveup: _ContextHandler | Iterable[_ContextHandler] | None = None,
    raise_on_giveup: bool = True,
    logger: _MaybeLogger = "backoff",
    backoff_log_level: int = logging.INFO,
    giveup_log_level: int = logging.ERROR,
    **wait_gen_kwargs: Any,
) -> AsyncGenerator[_Attempt, None]:
    """Async counterpart to `retry_context`, for use with `async for`.

        async for attempt in backoff.aretry_context(ValueError, backoff.expo):
            with attempt:
                await do_something()

    `giveup` and the handlers may be sync or async callables. See
    `retry_context` for the full argument reference.
    """
    logger = _prepare_logger(logger)
    on_success = _config_handlers(on_success)
    on_backoff = _config_handlers(
        on_backoff,
        default_handler=_log_backoff_context,
        logger=logger,
        log_level=backoff_log_level,
    )
    on_giveup = _config_handlers(
        on_giveup,
        default_handler=_log_giveup_context,
        logger=logger,
        log_level=giveup_log_level,
    )

    return _async.aretry_context(
        exception,
        wait_gen,
        max_tries=max_tries,
        max_time=max_time,
        jitter=jitter,
        giveup=giveup,
        on_success=on_success,
        on_backoff=on_backoff,
        on_giveup=on_giveup,
        raise_on_giveup=raise_on_giveup,
        wait_gen_kwargs=wait_gen_kwargs,
    )

Wait Generators

backoff.expo

expo(base=2, factor=1, max_value=None)

Generator for exponential decay.

Parameters:

Name Type Description Default
base float

The mathematical base of the exponentiation operation

2
factor float

Factor to multiply the exponentiation by.

1
max_value float | None

The maximum value to yield. Once the value in the true exponential sequence exceeds this, the value of max_value will forever after be yielded.

None
Source code in backoff/_wait_gen.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def expo(
    base: float = 2,
    factor: float = 1,
    max_value: float | None = None,
) -> Generator[float, Any, None]:
    """Generator for exponential decay.

    Args:
        base: The mathematical base of the exponentiation operation
        factor: Factor to multiply the exponentiation by.
        max_value: The maximum value to yield. Once the value in the
             true exponential sequence exceeds this, the value
             of max_value will forever after be yielded.
    """
    # Advance past initial .send() call
    yield 0

    a = factor
    while max_value is None or a < max_value:
        yield a
        a *= base
    while True:
        yield max_value

backoff.fibo

fibo(max_value=None)

Generator for fibonaccial decay.

Parameters:

Name Type Description Default
max_value int | None

The maximum value to yield. Once the value in the true fibonacci sequence exceeds this, the value of max_value will forever after be yielded.

None
Source code in backoff/_wait_gen.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def fibo(max_value: int | None = None) -> Generator[int, Any, None]:
    """Generator for fibonaccial decay.

    Args:
        max_value: The maximum value to yield. Once the value in the
             true fibonacci sequence exceeds this, the value
             of max_value will forever after be yielded.
    """
    # Advance past initial .send() call
    yield 0

    a = 1
    b = 1
    while max_value is None or a < max_value:
        yield a
        a, b = b, a + b
    while True:
        yield max_value

backoff.constant

constant(interval=1)

Generator for constant intervals.

Parameters:

Name Type Description Default
interval float | Iterable[float]

A constant value to yield or an iterable of such values.

1
Source code in backoff/_wait_gen.py
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def constant(interval: float | Iterable[float] = 1) -> Generator[float, Any, None]:
    """Generator for constant intervals.

    Args:
        interval: A constant value to yield or an iterable of such values.
    """
    # Advance past initial .send() call
    yield 0

    itr = (
        itertools.repeat(interval)
        if isinstance(interval, (int, float))
        else iter(interval)
    )

    for val in itr:
        yield val

backoff.runtime

runtime(*, value)

Generator that is based on parsing the return value or thrown exception of the decorated method

Parameters:

Name Type Description Default
value Callable[[Any], float]

a callable which takes as input the decorated function's return value or thrown exception and determines how long to wait

required
Source code in backoff/_wait_gen.py
102
103
104
105
106
107
108
109
110
111
112
113
def runtime(*, value: Callable[[Any], float]) -> Generator[float, Any, None]:
    """Generator that is based on parsing the return value or thrown
        exception of the decorated method

    Args:
        value: a callable which takes as input the decorated
            function's return value or thrown exception and
            determines how long to wait
    """
    ret_or_exc = yield 0
    while True:
        ret_or_exc = yield value(ret_or_exc)

Jitter Functions

backoff.full_jitter

full_jitter(value)

Jitter the value across the full range (0 to value).

This corresponds to the "Full Jitter" algorithm specified in the AWS blog's post on the performance of various jitter algorithms. (http://www.awsarchitectureblog.com/2015/03/backoff.html)

Parameters:

Name Type Description Default
value float

The unadulterated backoff value.

required
Source code in backoff/_jitter.py
16
17
18
19
20
21
22
23
24
25
26
def full_jitter(value: float) -> float:
    """Jitter the value across the full range (0 to value).

    This corresponds to the "Full Jitter" algorithm specified in the
    AWS blog's post on the performance of various jitter algorithms.
    (http://www.awsarchitectureblog.com/2015/03/backoff.html)

    Args:
        value: The unadulterated backoff value.
    """
    return random.uniform(0, value)

backoff.random_jitter

random_jitter(value)

Jitter the value a random number of milliseconds.

This adds up to 1 second of additional time to the original value. Prior to backoff version 1.2 this was the default jitter behavior.

Parameters:

Name Type Description Default
value float

The unadulterated backoff value.

required
Source code in backoff/_jitter.py
 4
 5
 6
 7
 8
 9
10
11
12
13
def random_jitter(value: float) -> float:
    """Jitter the value a random number of milliseconds.

    This adds up to 1 second of additional time to the original value.
    Prior to backoff version 1.2 this was the default jitter behavior.

    Args:
        value: The unadulterated backoff value.
    """
    return value + random.random()

Type Definitions

backoff.types

__all__ module-attribute

__all__ = ['Details']

Details

Bases: _BaseDetails, _CallDetails

Source code in backoff/_typing.py
28
29
class Details(_BaseDetails, _CallDetails, total=False):
    pass