Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #24618

retry many times decorator

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!novso.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <andrea.crotti.0@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'exception': 0.03; '%s"': 0.07; 'try:': 0.07; '**kwargs)': 0.09; '**kwargs):': 0.09; 'exception:': 0.09; 'fails.': 0.09; 'retry': 0.09; 'timeout': 0.09; 'def': 0.10; 'conn': 0.16; 'fatal': 0.16; 'perforce': 0.16; 'self.timeout': 0.16; 'skip:@ 20': 0.16; 'true:': 0.16; 'everyone,': 0.17; 'replacing': 0.17; 'bit': 0.21; 'skip:_ 20': 0.22; 'idea': 0.24; 'script': 0.24; 'tried': 0.25; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'run': 0.28; 'perl': 0.29; 'ret': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'function': 0.30; 'code': 0.31; '(and': 0.32; 'running': 0.32; 'could': 0.32; 'like:': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'false': 0.35; 'problem,': 0.35; 'sometimes': 0.35; 'there': 0.35; 'really': 0.36; 'except': 0.36; 'received:74.125': 0.36; 'problems': 0.36; 'skip:l 20': 0.38; 'things': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'first': 0.61; 'subject:times': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=ifPo9nxIVcREXoIeRBCWztabUnSfMGeBeuiM7BEQcWM=; b=Ircv9+WKqCnH1mcdaVTtRnk+h3hXZAB1k+WIYJdxYuJfhg8GlZkzGzgBWUP79S6U/y twmxl0YHIxgKewqq4vuDkpPg7SpexiXqOL+L/CMHdlYScQXJs0LOvq1IYeOlbbNhJFWS rX6AbQBk7H981Qdq8abcwQZJ7qCQoQ2AoGGEeVkDDS37AqcDyJObES6qFB0xvRsfgQeD FCGBUFHIkIgVkzafGDoa0XGJLd07Wd4wpthzmCbZw08SYPa1Xh1MUxR2MJYe4RDgaYfY ddI3xdV87ryvA80evi3RroQ9Gydt5gch+ZN8ECJb2fRVLL1r5P5U86gk/ROF4H+95h/X m7zQ==
MIME-Version 1.0
Date Thu, 28 Jun 2012 16:28:56 +0100
Subject retry many times decorator
From andrea crotti <andrea.crotti.0@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1609.1340897344.4697.python-list@python.org> (permalink)
Lines 61
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1340897344 news.xs4all.nl 6988 [2001:888:2000:d::a6]:52105
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:24618

Show key headers only | View raw


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
intervent..

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,
func.__name__))
                ret = func(*args, **kwargs)
                if ret:
                    return ret
                else:
                    sleep(self.timeout)

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

        return _retry_n_times

which can be used as

@retry_n_times(ntimes=10)
def connect():
    try:
        conn = mysql_connection()
    except Exception:
        return False
    else:
        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?

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

retry many times decorator andrea crotti <andrea.crotti.0@gmail.com> - 2012-06-28 16:28 +0100

csiph-web