Skip to content

Instantly share code, notes, and snippets.

@naufraghi
Created June 12, 2014 12:41
Show Gist options
  • Save naufraghi/db9214e2d5e02e18e90f to your computer and use it in GitHub Desktop.
Save naufraghi/db9214e2d5e02e18e90f to your computer and use it in GitHub Desktop.
Generic Python retry decorator
def retry(func, exception=Exception, num=3, wait=0.5):
"""
Example usage:
f = retry(open, IOError, num=3, wait=0.5)(file_path, 'w')
"""
def _retry(*args, **kwargs):
c = num
while True:
c = c-1
try:
return func(*args, **kwargs)
except exception:
if c > 0:
time.sleep(wait)
else:
raise
return _retry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment