Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4a.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; 'caller': 0.07; 'except:': 0.07; 'exception,': 0.09; 'logic': 0.09; 'raised,': 0.09; 'retry': 0.09; 'terminates': 0.09; 'timeout': 0.09; 'unexpected': 0.09; 'python': 0.11; 'assume': 0.11; '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; 'exceptions.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'function?': 0.16; 'hurts': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 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; 'wednesday': 0.18; ';-)': 0.18; '>>>': 0.20; 'assuming': 0.22; 'parameter': 0.22; 'terminate': 0.22; 'try:': 0.22; 'leave': 0.23; 'bit': 0.23; '2015': 0.23; 'ignored.': 0.23; 'skip:l 40': 0.23; 'header:In-Reply-To:1': 0.24; 'sort': 0.25; 'example': 0.25; 'header:User-Agent:1': 0.26; 'chris': 0.26; 'not,': 0.27; 'error': 0.27; 'have,': 0.27; "doesn't": 0.28; 'went': 0.28; 'actual': 0.29; 'catching': 0.29; 'cest': 0.29; 'correct,': 0.29; 'sleep': 0.29; 'windows,': 0.29; 'program,': 0.29; 'maybe': 0.31; "i'd": 0.31; 'seconds': 0.31; 'code': 0.31; 'gets': 0.32; 'up.': 0.32; 'implement': 0.32; 'received:84': 0.32; 'problem': 0.33; 'usually': 0.33; 'another': 0.34; 'message.': 0.34; 'skip:c 30': 0.35; 'wrong': 0.35; 'to:addr :python-list': 0.35; 'execution': 0.35; 'false': 0.35; 'instance': 0.35; 'but': 0.36; 'too': 0.36; 'except': 0.36; 'should': 0.37; 'subject:: ': 0.37; 'skip:g 20': 0.37; 'pm,': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'skip:t 20': 0.40; 'yes': 0.60; 'your': 0.60; 'here.': 0.61; 'skip:u 10': 0.62; 'better.': 0.66; 'burden': 0.66; 'apart': 0.70; 'useful.': 0.72; 'angelico:': 0.84; 'cecil': 0.84; 'schreef': 0.84; "that'll": 0.84; 'westerhof': 0.84; 'crucial': 0.91; 'interrupt': 0.91; 'same,': 0.91; 'cutting': 0.93 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=OoyysHLt c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=QrohdLjRRo4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=0kNUjEaEf2RiTkPByEgA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 Date: Wed, 03 Jun 2015 19:12:05 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Retrying to send message References: <87egltht87.fsf@Equus.decebal.nl> <876174ix9n.fsf@Equus.decebal.nl> In-Reply-To: <876174ix9n.fsf@Equus.decebal.nl> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit 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: 90 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433355137 news.xs4all.nl 2942 [2001:888:2000:d::a6]:50747 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:91982 On 2015-06-03 17:15, Cecil Westerhof wrote: > Op Wednesday 3 Jun 2015 15:29 CEST schreef Chris Angelico: > >> On Wed, Jun 3, 2015 at 10:27 PM, Cecil Westerhof wrote: >>> def send_message(account_id, message, max_tries, >>> terminate_program): error_msg = 'Something went wrong with: ' + >>> message not_send = True tries = 0 while not_send: try: >>> Core().update_status(account_id, message) except >>> libturpial.exceptions.ServiceOverCapacity: tries += 1 print('Tried >>> to send it {0} times'.format(tries)) if tries >= max_tries: >>> terminate_program(error_msg) time.sleep(60) except: >>> terminate_program(error_msg) else: not_send = 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 > > Yes that is much better. I now made it: > def send_message(account_id, message, max_tries, give_error, wait_time = 60): > error_msg = 'Something went wrong with: ' + message > tries = 0 > while True: > try: > Core().update_status(account_id, message) > break > except libturpial.exceptions.ServiceOverCapacity: > tries += 1 > print('Tried to send it {0} times'.format(tries)) > if tries >= max_tries: > give_error(error_msg) > return > time.sleep(wait_time) > except: > give_error(error_msg) > return > >> 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. > > I kept the except. I like to see the message that went wrong. ;-) > You're still swallowing unexpected exceptions. If any exception apart from ServiceOverCapacity is raised, the bare except will just call 'give_error' and then return. If you try to interrupt the program (^C on Windows, ^D on Linux), that'll also be ignored. > Also, in my case give_error terminates the program, but it never hurts > to make functionality generic, so I added return statements. Not > break, because on error you should leave the function. Break would in > this instance do the same, but I find this neater. > > >>> 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’s 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. > > Correct, so I did not assume to much. :-D > > Usually I check parameters. But I understood that is not the Python > way. For example a wait_time of 1, or negative is not useful. Also a > max_tries of 10 ** 12 would not be very useful. But I can put the > burden of the problem on the caller of the function? >