tenacity icon indicating copy to clipboard operation
tenacity copied to clipboard

Question: How to handle dynamically changing retry (think api throttling)

Open ravensorb opened this issue 1 year ago • 3 comments

I am curious if there is a way to handle dynamically change the retry wait period based on something that happens within the process being executed. The specific example I have is an API that has a rate limit on it and it returns in the HTTP headers the recommended amount of time to wait before retying the call.

Any thoughts or suggestions on how to approach?

ravensorb avatar May 14 '24 22:05 ravensorb

I'm also interested in this!

ahgraber avatar Jun 27 '24 19:06 ahgraber

you can use a custom wait callback:

def my_wait(retry_state: RetryCallState):
    ex = retry_state.outcome.exception()
    if isinstance(ex, requests.exceptions.HTTPError) and ex.response.status_code == 429:
        retry_after = ex.response.headers.get("Retry-After")
        try:
            return int(retry_after)
        except (TypeError, ValueError):
            pass
    return 0

basilisk487 avatar Sep 24 '24 20:09 basilisk487