Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!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; 'tries': 0.05; 'except:': 0.07; 'exception,': 0.09; 'logic': 0.09; 'retry': 0.09; 'timeout': 0.09; 'cc:addr:python-list': 0.10; 'exception': 0.13; 'ignore': 0.14; 'def': 0.14; 'wed,': 0.15; 'better?': 0.16; 'bug,': 0.16; 'clause.': 0.16; 'error_msg': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject:send': 0.16; 'true:': 0.16; 'useless': 0.16; '{0}': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'else,': 0.18; 'skip': 0.18; ';-)': 0.18; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'assuming': 0.22; 'parameter': 0.22; 'terminate': 0.22; 'try:': 0.22; 'bit': 0.23; '2015': 0.23; 'skip:l 40': 0.23; 'header:In- Reply-To:1': 0.24; 'sort': 0.25; 'not,': 0.27; 'have,': 0.27; 'message-id:@mail.gmail.com': 0.28; "doesn't": 0.28; 'went': 0.28; 'actual': 0.29; 'catching': 0.29; 'sleep': 0.29; 'maybe': 0.31; "i'd": 0.31; 'seconds': 0.31; 'code': 0.31; 'gets': 0.32; 'up.': 0.32; 'implement': 0.32; 'another': 0.34; 'message.': 0.34; 'received:google.com': 0.34; 'skip:c 30': 0.35; 'wrong': 0.35; 'execution': 0.35; 'false': 0.35; 'too': 0.36; 'except': 0.36; 'should': 0.37; 'subject:: ': 0.37; 'pm,': 0.39; 'does': 0.39; 'skip:t 20': 0.40; 'your': 0.60; 'here.': 0.61; 'skip:u 10': 0.62; 'cecil': 0.84; 'chrisa': 0.84; 'westerhof': 0.84; 'to:none': 0.90; 'crucial': 0.91; 'cutting': 0.93 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:cc :content-type:content-transfer-encoding; bh=lZ7m7hKJvn/Rb/oAiWVAat0uwr+DoF/T3JY167D/AB0=; b=dY9r9bWSZGytoi9IbNvr9OtowdKj0MBq0ldjGayPkwESzfPNuLWQhTaw96FM1QjmNH uvGwT9Un1LgH9eQ9yoNcM184BAPlFvE5eSDllxTW35QxgXoP0VdG3PeGyeDKUhV/8GBk 8+dqk7AuDxC78ax3btFjKQNcMSv/dKL8w88lBY74d+p9rNPEOIHez4b0RyrQl84ojjWj lFOlzjX1CcZv9ukfpIQytkBwq5mNu4Tgb09/PsXh2kYYSfapXhPZ0D3Pk4uYnuN7ovgu VJeCiNP3QhHBniN8h4MZLydOiLqV/GotqHmq91lq8HNLItaeuRbESznpqDbkp4RhVjNI j7JA== MIME-Version: 1.0 X-Received: by 10.50.143.104 with SMTP id sd8mr14487257igb.14.1433338142255; Wed, 03 Jun 2015 06:29:02 -0700 (PDT) In-Reply-To: <87egltht87.fsf@Equus.decebal.nl> References: <87egltht87.fsf@Equus.decebal.nl> Date: Wed, 3 Jun 2015 23:29:02 +1000 Subject: Re: Retrying to send message From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433338145 news.xs4all.nl 2842 [2001:888:2000:d::a6]:44543 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:91962 On Wed, Jun 3, 2015 at 10:27 PM, Cecil Westerhof wrote: > def send_message(account_id, message, max_tries, terminate_program): > error_msg =3D 'Something went wrong with: ' + message > not_send =3D True > tries =3D 0 > while not_send: > try: > Core().update_status(account_id, message) > except libturpial.exceptions.ServiceOverCapacity: > tries +=3D 1 > print('Tried to send it {0} times'.format(tries)) > if tries >=3D max_tries: > terminate_program(error_msg) > time.sleep(60) > except: > terminate_program(error_msg) > else: > not_send =3D False > > Is this a reasonable way to implement this, or is another way better? > Well, maybe the timeout should be a parameter also. ;-) I'd skip the not_send flag and do the logic thusly: while True: try: update_status as above break except ServiceOverCapacity: as above And I'd also skip the bare except clause. If you get any sort of exception, whether it's a bug, a failure from libturpial, a network error, or anything else, your code will just terminate with a bland and useless message. Much better to simply let the exception bubble up. > Also the documentation of time.sleep says: > The actual suspension time may be less than that requested because > any caught signal will terminate the sleep() following execution > of that signal=E2=80=99s catching routine. > > My program does not much else as sending the message. So I think it is > not necessary to cover for this possibility. Are I assuming to much? I wouldn't worry too much about a signal cutting your sleep short; it doesn't look to me as if "exactly sixty seconds" is all that crucial here. If your program sleeps for 42.645 seconds and then gets woken up by a signal, and you retry a bit sooner than you otherwise would have, is it going to break anything? If not, just ignore that possibility. ChrisA