Comments (2)
I was looking for how to change the initial delay, and it took a bit to figure out that factor
and/or base
as they are exposed, so it is possible to change them, at least for the exponential backoff. There is no example on how to do it in README.md, so I thought I'd post a short one in this thread. It is not exactly what this thread is asking for, but it gives some level of control over the initial delay.
backoff_example.py
:
import backoff
def backoff_handler(details):
print(
"Backing off {wait:0.1f} seconds after {tries} tries "
"calling function {target} with args {args} and kwargs "
"{kwargs}".format(**details)
)
@backoff.on_exception(
backoff.expo,
Exception,
max_tries=5,
on_backoff=backoff_handler,
jitter=None,
factor=2,
)
def myfunc():
print("called myfunc")
raise Exception("myfunc failed")
if __name__ == "__main__":
myfunc()
$ python backoff_example.py
called myfunc
Backing off 2.0 seconds after 1 tries calling function <function myfunc at 0x10da554d0> with args () and kwargs {}
called myfunc
Backing off 4.0 seconds after 2 tries calling function <function myfunc at 0x10da554d0> with args () and kwargs {}
called myfunc
Backing off 8.0 seconds after 3 tries calling function <function myfunc at 0x10da554d0> with args () and kwargs {}
called myfunc
Backing off 16.0 seconds after 4 tries calling function <function myfunc at 0x10da554d0> with args () and kwargs {}
called myfunc
Traceback (most recent call last):
File "backoff_example.py", line 26, in <module>
myfunc()
File "/Users/sryabkov/.asdf/installs/python/3.7.13/lib/python3.7/site-packages/backoff/_sync.py", line 105, in retry
ret = target(*args, **kwargs)
File "backoff_example.py", line 22, in myfunc
raise Exception("myfunc failed")
Exception: myfunc failed
from backoff.
For anyone coming to this issue as I did, this is the approach I took for this:
def _min_expo_wait(min_wait: float):
"""Exponential backoff with a minimum wait time."""
def f(*args, **kwargs):
yield max(min_wait, next(backoff.expo(*args, **kwargs), min_wait))
return f
@backoff.on_exception(
_min_expo_wait(60.0),
some.Exception,
max_tries=3,
)
The original proposed formula do not match with my expectation of exponential wait with a minimum, the code below will follow this one instead:
a = max(minimum, factor * base ** n)
from backoff.
Related Issues (20)
- Duplicate logging in some special cases HOT 1
- How do I use this?
- Obtain value returned by backoff_handler function after maximum tries is reached
- max_retries HOT 1
- max_time only applies _after_ first retry?
- Canonical way to handle error bursting
- Add exponential decay
- How to handle backoff on Facebook cursor? HOT 1
- Improve Typing and Error Handling in `on_exception`
- question about max_time
- Feature Request: add a way to use backoff in a context manager HOT 2
- feature request: toggle decorator on/off
- `backoff.on_predicate` does not work for `staticmethod`s
- Expose typing hints as they are part of the API
- Add support for globally disabling retries
- How to provide arguments to `on_backoff` function HOT 1
- Feature Request: support direct invocation with functools.partial
- Maintenance help? HOT 7
- README: `max_value` arg to `@backoff.on_predicate(backoff.fibo...)` -- if not obsolete, then maybe just confusing?
- Prevent error message in case of giveup using
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from backoff.