How to retry in Python

1 min readAug 26


Let’s find out how to use retry logic in Python.

Main code

def retryable(total_try_cnt=5, sleep_in_sec=0, retryable_exceptions=()):
Usage: Specify exception to retry in method to retry and use as decorator
Retry total_try_cnt number of times, Raise if error occurs at the end

def decorator(func):
def wrapper(*args, **kwargs):
for cnt in range(total_try_cnt):"trying {func.__name__}() [{cnt+1}/{total_try_cnt}]")

result = func(*args, **kwargs)"{func.__name__}() ran success")

if result:
return result
except retryable_exceptions as e:"Error while retryable, {func.__name__}() raised retryable exception '{e}'")
if cnt >= total_try_cnt - 1:
raise e
except Exception as e:"Error while retryable, {func.__name__}() raised {e}")
raise e

time.sleep(sleep_in_sec)"{func.__name__} finally has been failed")

return wrapper

return decorator


import retryable
import requests

@retryable(total_try_cnt=3, retryable_exceptions=(HttpError))
def api_call():
return requests.get("").json()

If you want more functions like backoff, jitter, and so on, I recommend the famous library tenacity. In my case, I just wanted a simple function. This code is enough to me.

Please clap if you liked this posting. Thank you.