Last active
March 14, 2022 13:21
-
-
Save oiao/3bacf1f23e51daae9a5156cd3a79aa65 to your computer and use it in GitHub Desktop.
python-snippets
This file contains 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
""" | |
Retry a function after `sleep` secionds for a max of `maxtries` | |
""" | |
def retry(sleep=0.1, maxtries=10): | |
# wrapper for sleep-retrying a function call. use with `@retry()` | |
from time import sleep as _sleep | |
def wrap1(f): | |
def wrap2(*a, __count=0, **kw): | |
try: | |
return f(*a, **kw) | |
except Exception as e: | |
if __count > maxtries: | |
raise e from None # ignores stack trace | |
_sleep(sleep) | |
wrap2(*a, __count=__count+1, **kw) | |
return wrap2 | |
return wrap1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment