Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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; 'exception:': 0.09; 'fails.': 0.09; 'propagate': 0.09; 'retry': 0.09; 'timeout': 0.09; 'def': 0.10; 'boolean': 0.16; 'conn': 0.16; 'fatal': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'perforce': 0.16; 'self.timeout': 0.16; 'skip:@ 20': 0.16; 'swallow': 0.16; 'true:': 0.16; 'wrote:': 0.17; 'everyone,': 0.17; 'replacing': 0.17; '(or': 0.18; 'bit': 0.21; 'permitted': 0.22; 'skip:_ 20': 0.22; 'specified': 0.23; 'idea': 0.24; 'script': 0.24; 'tried': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; "doesn't": 0.28; 'run': 0.28; 'perl': 0.29; 'received:192.168.1.3': 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; 'false': 0.35; 'problem,': 0.35; 'returning': 0.35; 'sometimes': 0.35; 'there': 0.35; 'really': 0.36; 'except': 0.36; 'problems': 0.36; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'things': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'skip:u 10': 0.60; 'first': 0.61; 'maximum': 0.63; 'header:Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'andrea': 0.84; 'reply-to:addr:python.org': 0.84; 'subject:times': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=KqrPKBqN c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=YsUzL_8ObRgA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=duEXqyrFMXzac8QjsDEA:9 a=wPNLvfGTeEIA:10 a=8oZvZ2yz5jbZbLzm:21 a=9BZrp7h8ZjzaPSL_:21 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Thu, 28 Jun 2012 17:12:40 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: retry many times decorator References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org 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: 67 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1340899961 news.xs4all.nl 6926 [2001:888:2000:d::a6]:39052 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:24620 On 28/06/2012 16:28, andrea crotti wrote: > 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? > 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.