Last active
December 18, 2015 20:29
-
-
Save trhura/5840354 to your computer and use it in GitHub Desktop.
Python decorator to retry function calls on Exception
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
import functools | |
def retry (function, retry=3): | |
@functools.wraps(function) | |
def wrapper (*args, **kwargs): | |
exception = None | |
for i in range (retry): | |
try: | |
return function (*args, **kwargs) | |
except Exception, ex: | |
exception = ex | |
raise exception | |
return wrapper | |
@retry | |
def retry_test (): | |
raise Exception ('Test Exception') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment