retry many times decorator

andrea crotti andrea.crotti.0 at
Thu Jun 28 17:28:56 CEST 2012

Hi everyone, I'm replacing a perl system that has to work a lot with
databases and perforce (and a few other things).

This script have to run completely unsupervisioned, so it's important
that it doesn't just quit at the first attempt waiting for human

They say that the network sometimes has problems so all over the code
there are things like:
until ($dbh = DBI->connect('...'))
    sleep( 5 * 60 );

Since I really don't want to do that I tried to do a decorator:

class retry_n_times:
    def __init__(self, ntimes=3, timeout=3, fatal=True):
        self.ntimes = ntimes
        self.timeout = timeout
        self.fatal = fatal

    def __call__(self, func):
        def _retry_n_times(*args, **kwargs):
            attempts = 0
            while True:
                logger.debug("Attempt number %s of %s" % (attempts,
                ret = func(*args, **kwargs)
                if ret:
                    return ret

                attempts += 1
                if attempts == self.ntimes:
                    logger.error("Giving up the attempts while running
%s" % func.__name__)
                    if self.fatal:

        return _retry_n_times

which can be used as

def connect():
        conn = mysql_connection()
    except Exception:
        return False
        return True

So the function to be decorated has to return a boolean..  The problem
is that I would like to keep the exception message to report a bit
better what could be the problem, in case the retry fails.

Any idea about how to improve it?

More information about the Python-list mailing list