tenacity
tenacity copied to clipboard
max wait doesn't stop the retries
Hey
I have configure my function like so:
@retry(wait=wait_exponential(multiplier=1, max=6),
stop=retry_if_result(lambda state: isinstance(state, ValueError)),
retry_error_callback=log_exception,
after=after_log(logger, logging.WARNING))
However, when the waiting time is exceeded the function is still continuing. What am I doing wrong here?
You did not set any condition to stop based on any max time.
the max= in wait_exponential is just the maximum wait time between 2 retries.
I finally figured it out that I had to explicitly add a stop condition. This doesn't seem intuitive to me. I would expect the function to return once a non-exception is returned.
This is what worked for me, for reference for future users:
@retry(
wait=wait_random_exponential(multiplier=1, max=15),
stop=stop_after_attempt(7),
retry=retry_if_not_result(lambda r: isinstance(r, (dict, ValueError))),
retry_error_callback=lambda retry_state: logger.warning(
f"Retry state: {retry_state.outcome.result()}"
),
after=after_log(logger, logging.WARNING),
)
To achieve the following:
- we re-try the function in exponential increments of 2^x * 1, up to max X seconds between attempts
- we stop after 7 attempts or if we return a dict or a ValueError
- we log the last result
- we log the retry attempt #