Skip to content

Instantly share code, notes, and snippets.

@mikelikespie
Created April 4, 2010 22:35
Show Gist options
  • Save mikelikespie/355762 to your computer and use it in GitHub Desktop.
Save mikelikespie/355762 to your computer and use it in GitHub Desktop.
def retries(num_retries, exception_types=Exception, check_func=None, timeout=0.0):
"""retries a function ``num_retries`` times.
will catch ``exception_types`` of exceptions. This can be a tuple as well.
check_func can optionally be provided. It takes the exception as an argument.
If it returns a value that evaluates to false, the exception will be reraised.
can be used like:
@retries(10, urllib2.HTTPError, lambda e: e.errno == 404)
def foo():
#do stuff
it will only retry if it's a 404.
"""
def retries_decorator(fn):
def inner_func(*args, **kwargs):
for current_try in range(num_retries + 1):
try:
return fn(*args, **kwargs)
except exception_types, e:
if current_try < num_retries and \
(not check_func or check_func(e)):
print >>sys.stderr, "receive exception: %s" % e
print >>sys.stderr, "will retry %d more times" % \
(num_retries - current_try)
if timeout and timeout > 0.0:
print >>sys.stderr, "sleeping for %0.04f seconds before retrying" % timeout
time.sleep(timeout)
else:
#If we maxed out our thing, just raise it
raise
return inner_func
return retries_decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment