Skip to content

Instantly share code, notes, and snippets.

@oiao
Last active March 14, 2022 13:21
Show Gist options
  • Save oiao/3bacf1f23e51daae9a5156cd3a79aa65 to your computer and use it in GitHub Desktop.
Save oiao/3bacf1f23e51daae9a5156cd3a79aa65 to your computer and use it in GitHub Desktop.
python-snippets
"""
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