Created
May 25, 2022 08:34
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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