How to retry in Python

chrisjune
1 min readAug 26, 2023

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):
@functools.wraps(func)
def wrapper(*args, **kwargs):
for cnt in range(total_try_cnt):
log.info(f"trying {func.__name__}() [{cnt+1}/{total_try_cnt}]")

try:
result = func(*args, **kwargs)
log.info(f"{func.__name__}() ran success")

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

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

return wrapper

return decorator

Usage

import retryable
import requests

@retryable(total_try_cnt=3, retryable_exceptions=(HttpError))
def api_call():
return requests.get("https://google.com").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.

--

--