Skip to content

Instantly share code, notes, and snippets.

@mrunderline
Created May 25, 2022 08:34
Show Gist options
  • Save mrunderline/679ce5d92ec8e05fa6db30e69298b737 to your computer and use it in GitHub Desktop.
Save mrunderline/679ce5d92ec8e05fa6db30e69298b737 to your computer and use it in GitHub Desktop.
this is a decorator that help you to retry on specific exceptions dynamically
import time
def retry(exceptions, max_tries=5, delay=1):
def decorator(function):
def wrapper(*args, **kwargs):
attempt = 0
while attempt < max_tries:
try:
return function(*args, **kwargs)
except exceptions as err:
print(f'Exception thrown when attempting to run {function}, attempt {attempt} of {max_tries}')
print(err)
attempt += 1
time.sleep(delay)
return wrapper
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment