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
|
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 | |
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 | |
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
|
None
|
on_backoff
|
_ContextHandler | Iterable[_ContextHandler] | None
|
Callable (or iterable of callables) called on
backoff. The parameter dict additionally has |
None
|
on_giveup
|
_ContextHandler | Iterable[_ContextHandler] | None
|
Callable (or iterable of callables) called when
giving up. The parameter dict additionally has |
None
|
raise_on_giveup
|
bool
|
Boolean indicating whether the registered
exception should be raised on giveup. Defaults to |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
Type Definitions
backoff.types
__all__
module-attribute
__all__ = ['Details']
Details
Bases: _BaseDetails, _CallDetails
Source code in backoff/_typing.py
28 29 | |