Created
June 12, 2014 12:41
-
-
Save naufraghi/db9214e2d5e02e18e90f to your computer and use it in GitHub Desktop.
Generic Python retry decorator
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
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