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
|
|
A generator yielding successive wait times in seconds. |
required |
exception
|
|
An exception type (or tuple of types) which triggers backoff. |
required |
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. |
None
|
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. |
None
|
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. |
lambda e: False
|
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. |
None
|
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. |
None
|
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. |
None
|
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'
|
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. |
{}
|
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 | |
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
|
|
A generator yielding successive wait times in seconds. |
required |
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. |
None
|
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. |
None
|
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. |
None
|
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. |
None
|
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. |
None
|
logger
|
|
Name of logger or Logger object to log to. Defaults to 'backoff'. |
'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. |
{}
|
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 | |
Wait Generators
expo
backoff.expo(base=2, factor=1, max_value=None)
Generator for exponential decay.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
|
The mathematical base of the exponentiation operation |
2
|
factor
|
|
Factor to multiply the exponentiation by. |
1
|
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. |
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 | |
fibo
backoff.fibo(max_value=None)
Generator for fibonaccial decay.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
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. |
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 | |
constant
backoff.constant(interval=1)
Generator for constant intervals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interval
|
|
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 | |
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
|
|
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 | |
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
|
|
The unadulterated backoff value. |
required |
Source code in backoff/_jitter.py
16 17 18 19 20 21 22 23 24 25 26 | |
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
|
|
The unadulterated backoff value. |
required |
Source code in backoff/_jitter.py
4 5 6 7 8 9 10 11 12 13 | |
Type Definitions
backoff.types
__all__ = ['Details']
module-attribute
Details
Bases:
Source code in backoff/_typing.py
25 26 27 28 | |