I would like to be be able to use the backoff library to decorate a Callable created using functools.partial so that I can:
specify some of backoff's parameters dynamically, such as max_time, and
use backoff in my own wrapper function (which accepts a Callable, for which functools.partial is preferred over a lambda in general for its repr() functionality).
Example of proposed backoff usage with functools.partial:
Currently, this is close to working, but it fails to generate the "backing off" log message (with backoff 2.2.1 and Python 3.10.6):
File "/.../lib/python3.10/site-packages/backoff/_common.py", line 97, in _log_backoff
log_args = [details['target'].__name__, details['wait']]
AttributeError: 'functools.partial' object has no attribute '__name__'
A Python lambda can be used as a workaround (run_http_client_operation_with_retries(lambda: foobar(baz=123))), but then the log message is unclear (Backing off <lambda>(...) ...). One advantage of functools.partial is that it provides a clearer function name when you do repr(functools.partial(...)).
Using backoff as a context manager was proposed in #198 and #18. That would solve the same problem of being able to specify backoff parameters dynamically. However, it's impossible to implement a context manager that re-runs the code block it manages (see https://github.com/litl/backoff/issues/18#issuecomment-262261996).
I would like to be be able to use the
backoff
library to decorate aCallable
created usingfunctools.partial
so that I can:backoff
's parameters dynamically, such asmax_time
, andbackoff
in my own wrapper function (which accepts aCallable
, for whichfunctools.partial
is preferred over a lambda in general for itsrepr()
functionality).Example of proposed
backoff
usage withfunctools.partial
:Currently, this is close to working, but it fails to generate the "backing off" log message (with backoff 2.2.1 and Python 3.10.6):
A Python lambda can be used as a workaround (
run_http_client_operation_with_retries(lambda: foobar(baz=123))
), but then the log message is unclear (Backing off <lambda>(...) ...
). One advantage offunctools.partial
is that it provides a clearer function name when you dorepr(functools.partial(...))
.Using backoff as a context manager was proposed in #198 and #18. That would solve the same problem of being able to specify backoff parameters dynamically. However, it's impossible to implement a context manager that re-runs the code block it manages (see https://github.com/litl/backoff/issues/18#issuecomment-262261996).