python-3.xexponential-backoff

Decorator backoff.on_predicate not waiting as expected


I'm checking the constant interval between calls and found, that in this infinite loop, the time between consecutive calls is not 5 seconds and varies by random, though less than 5 sec. Don't understand, why.

from datetime import datetime
from backoff import on_predicate, constant

@on_predicate(constant, interval=5)
def fnc(i):
    print('%s %d' % (datetime.now().strftime("%H:%M:%S:%f"),i), flush=True)
    return i

for i in range(7):
    fnc(i)

Output:

17:48:48:348775 0
17:48:50:898752 0
17:48:52:686353 0
17:48:53:037900 0
17:48:57:264762 0
17:48:58:348803 0

Solution

  • The backoff library uses a jitter function to randomize the interval. It's normally what you want when doing exponential backoff or similar, but might be surprising when using the constant wait generator. To disable jitter, specify jitter=None:

    @on_predicate(constant, interval=5, jitter=None)
    def fnc(i):
        print('%s %d' % (datetime.now().strftime("%H:%M:%S:%f"),i), flush=True)
        return i