GithubHelp home page GithubHelp logo

Minimum delay about backoff HOT 2 OPEN

litl avatar litl commented on August 23, 2024 14
Minimum delay

from backoff.

Comments (2)

sryabkov avatar sryabkov commented on August 23, 2024 6

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.

photonbit avatar photonbit commented on August 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.