# 2013-03-06 johnpfeiffer
import time
from functools import wraps
def retry( ExceptionToCheck , tries = 4 , delay = 1 , backoff = 2 , logger = None ):
"""Retry calling the decorated function using an exponential backoff.
http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/
original from: http://wiki.python.org/moin/PythonDecoratorLibrary#Retry
:param ExceptionToCheck: the exception to check. may be a tuple of
exceptions to check
:type ExceptionToCheck: Exception or tuple
:param tries: number of times to try (not retry) before giving up
:type tries: int
:param delay: initial delay between retries in seconds
:type delay: int
:param backoff: backoff multiplier e.g. value of 2 will double the delay
each retry
:type backoff: int
:param logger: logger to use. If None, print
:type logger: logging.Logger instance
"""
def deco_retry( f ):
@wraps( f )
def f_retry( *args , **kwargs ):
mtries, mdelay = tries, delay
while mtries > 1:
try:
return f( *args , **kwargs )
except ExceptionToCheck, e:
message = "%s, Retrying in %d seconds..." % ( str(e) , mdelay )
if logger:
logger.warning( message )
else:
print message
time.sleep( mdelay )
mtries -= 1
mdelay *= backoff
return f( *args , **kwargs )
return f_retry # true decorator
return deco_retry
"""
class Retry( object ):
defaultexceptions = (Exception,)
def init( self , tries , exceptions = None , delay = 0 ): # todo backoff = 2 (exponential backoff)
"" "
Decorator for retrying a function if exception occurs
tries -- number of tries
exceptions -- exceptions to catch
delay -- wait between retries
"" "
self.tries = tries
if exceptions is None:
exceptions = Retry.defaultexceptions
self.exceptions = exceptions
self.delay = delay
def _call( self , f ):
def fn(args, *kwargs):
exception = None
for i in range( self.tries ):
try:
return f( args , *kwargs )
except self.exceptions, e:
print 'RETRY after delay %s, exception: %s' % ( self.delay , str( e ) )
time.sleep( self.delay )
exception = e
#if no success after tries, raise last exception
raise exception
return fn
"""