litl / backoff

Python library providing function decorators for configurable backoff and retry
MIT License
2.61k stars 148 forks source link

How to provide arguments to `on_backoff` function #208

Open vk496 opened 1 year ago

vk496 commented 1 year ago

Hello and thank you for this amazing project.

Im experimenting with it and I miss be able to use arguments to the backoff functions. For example:


async def backoff_f(arg1: int):
  pass

@backoff.on_exception(wait_gen=backoff.expo, exception=ValueError, on_backoff=backoff_f(arg1=2), max_tries=5)
@backoff.on_exception(wait_gen=backoff.expo, exception=RuntimeError, on_backoff=backoff_f(arg1=6), max_tries=5)
async def test1() -> int:
  pass

Is that possible?

Thank you in advance

evgenybf commented 9 months ago

It's likely you need "partial" functions

>>> from functools import partial
>>> def a(arg1: int):
...    print(arg1)
...

>>> a_with_2 = partial(a, 2)
>>> a_with_2()
2