Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'exception.': 0.07; 'raised': 0.07; 'try:': 0.07; '**kwargs)': 0.09; '**kwargs):': 0.09; 'propagate': 0.09; 'raised.': 0.09; 'timeout': 0.09; 'def': 0.10; 'boolean': 0.16; 'fatal': 0.16; 'it..': 0.16; 'self.timeout': 0.16; 'swallow': 0.16; 'true:': 0.16; '(or': 0.18; 'permitted': 0.22; 'received :mail-bk0-f46.google.com': 0.22; 'skip:_ 20': 0.22; 'specified': 0.23; 'header:In-Reply-To:1': 0.25; 'received:209.85.214.46': 0.27; 'message-id:@mail.gmail.com': 0.27; '(since': 0.29; 'ret': 0.29; 'url:mailman': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'error': 0.30; 'url:python': 0.32; 'running': 0.32; 'could': 0.32; 'url:listinfo': 0.32; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'returning': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'except': 0.36; 'url:org': 0.36; "didn't": 0.36; 'should': 0.36; 'possible': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'header:Received:5': 0.40; 'url:mail': 0.40; 'below,': 0.60; 'maximum': 0.63; 'become': 0.65; 'subject:times': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=VB70Z5krs7ewYOhPOzS47uYCekcJ9MGQvyd8mPd1xig=; b=hQt0PrHd6tGz9nghPaOm0DgKG4vp/lilbuaBbKJ1PfvyJ0Y8PUArxQdoWW2r8df2Gt gy5ANtlYZUq+EmJ8SDPTkoSt4mPJZnfJ4bJaBi4KqJ65XjEiZyCBPszlMRTGb4BrrR6s HIH0gzKcIqQtOP5JolxpS3MMjeLllTk5vgTrhw3/WzKXHiQUSw68geY8b9eJLB4PW+EV FhmWmwZUdudN/G+RLjc8bDkCKyyW0JlYW4MMgsFeSkHfZzE4ZN4vksoB3xfSR9/pmb8K +MQxMhdzcZYRovlOGXr2oO7zrnMRRXIe0pwpC/K0luW1ZFu4fV8gp+A325NBv4jBrZyU 1zFQ== MIME-Version: 1.0 In-Reply-To: <4FEC8278.7020806@mrabarnett.plus.com> References: <4FEC8278.7020806@mrabarnett.plus.com> Date: Thu, 28 Jun 2012 17:26:36 +0100 Subject: Re: retry many times decorator From: andrea crotti 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1340900799 news.xs4all.nl 6856 [2001:888:2000:d::a6]:43667 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:24621 > Returning a boolean isn't very Pythonic. It would be better, IMHO, if > it could swallow a specified exception (or specified exceptions?) > raised when an attempt failed, up to the maximum permitted number of > attempts. If the final attempt fails, propagate the exception. > -- > http://mail.python.org/mailman/listinfo/python-list Yes right I also didn't like it.. Now it become something as below, so I capture every possible exception (since it must be generic) and log what exception was raised. I'm not re-raising because if it fails and it's fatal I should just exit, and if it's not fatal it should just continue.. 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__)) try: ret = func(*args, **kwargs) except Exception as e: logger.error("Got exception %s with error %s" % (type(e), str(e))) sleep(self.timeout) else: return ret 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