backoff
backoff copied to clipboard
Measure elapsed time after function call
This patch fixes what I consider to be unexpected behavior:
Steps to reproduce
import time
import backoff
@backoff.on_exception(backoff.constant, RuntimeError, jitter=None, max_time=1)
def foo():
print("Running")
time.sleep(2)
raise
def real_time():
start = time.time()
try:
foo()
except:
pass
print(time.time() - start)
real_time()
Observed behavior
$ poetry run python foo.py
Running
Running
5.004697322845459
Expected behavior
When I specify the max_time
argument, I expect the worst-case upper bound for execution time to be the specified timeout + the execution time of a single function call. Moreover, I would not expect retries to trigger if the give up condition is already fulfilled after the first function execution.
This behavior appears to be caused by collected elapsed
for the details
before the target function is executed. This patch should fix both of the above bugs by swapping that ordering.
This would have addressed #186
I forked the library as https://github.com/Kirusi/improved_backoff. If you depend on properly working max_time
feel free to use it.