Skip to content

API Reference

Complete API documentation for the backoff module.

Decorators

on_exception

backoff.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 _MaybeSequence[Type[Exception]]

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

required
max_tries Optional[_MaybeCallable[int]]

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 Optional[_MaybeCallable[float]]

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 Union[_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 Union[_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 Union[_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 Union[_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
def on_exception(
    wait_gen: _WaitGenerator,
    exception: _MaybeSequence[Type[Exception]],
    *,
    max_tries: Optional[_MaybeCallable[int]] = None,
    max_time: Optional[_MaybeCallable[float]] = None,
    jitter: Union[_Jitterer, None] = full_jitter,
    giveup: _Predicate[Exception] = lambda e: False,
    on_success: Union[_Handler, Iterable[_Handler], None] = None,
    on_backoff: Union[_Handler, Iterable[_Handler], None] = None,
    on_giveup: Union[_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[[_CallableT], _CallableT]:
    """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):
        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

on_predicate

backoff.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 Optional[_MaybeCallable[int]]

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 Optional[_MaybeCallable[float]]

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 Union[_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 Union[_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 Union[_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 Union[_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
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 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
def on_predicate(
    wait_gen: _WaitGenerator,
    predicate: _Predicate[Any] = operator.not_,
    *,
    max_tries: Optional[_MaybeCallable[int]] = None,
    max_time: Optional[_MaybeCallable[float]] = None,
    jitter: Union[_Jitterer, None] = full_jitter,
    on_success: Union[_Handler, Iterable[_Handler], None] = None,
    on_backoff: Union[_Handler, Iterable[_Handler], None] = None,
    on_giveup: Union[_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[[_CallableT], _CallableT]:
    """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):
        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

Wait Generators

expo

backoff.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 Optional[float]

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
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def expo(
    base: float = 2,
    factor: float = 1,
    max_value: Optional[float] = 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  # type: ignore[misc]
    base_n: float = 1
    while True:
        a = factor * base_n
        if max_value is None or a < max_value:
            yield a
            base_n *= base
        else:
            yield max_value

fibo

backoff.fibo(max_value=None)

Generator for fibonaccial decay.

Parameters:

Name Type Description Default
max_value Optional[int]

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def fibo(max_value: Optional[int] = None) -> Generator[int, None, 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  # type: ignore[misc]

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

constant

backoff.constant(interval=1)

Generator for constant intervals.

Parameters:

Name Type Description Default
interval Union[int, Iterable[float]]

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

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

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

    try:
        itr = iter(interval)  # type: ignore
    except TypeError:
        itr = itertools.repeat(interval)  # type: ignore

    for val in itr:
        yield val

runtime

backoff.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def runtime(
    *,
    value: Callable[[Any], float],
) -> Generator[float, None, 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  # type: ignore[misc]
    while True:
        ret_or_exc = yield value(ret_or_exc)

Jitter Functions

full_jitter

backoff.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)

random_jitter

backoff.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__ = ['Details'] module-attribute

Details

Bases: _Details

Source code in backoff/_typing.py
25
26
27
28
class Details(_Details, total=False):
    wait: float  # present in the on_backoff handler case for either decorator
    value: Any  # present in the on_predicate decorator case
    exception: Exception  # present in the on_exception decorator case