Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #91962
| References | <87egltht87.fsf@Equus.decebal.nl> |
|---|---|
| Date | 2015-06-03 23:29 +1000 |
| Subject | Re: Retrying to send message |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.108.1433338144.13271.python-list@python.org> (permalink) |
On Wed, Jun 3, 2015 at 10:27 PM, Cecil Westerhof <Cecil@decebal.nl> 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
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’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.
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Retrying to send message Cecil Westerhof <Cecil@decebal.nl> - 2015-06-03 14:27 +0200
Re: Retrying to send message Chris Angelico <rosuav@gmail.com> - 2015-06-03 23:29 +1000
Re: Retrying to send message Cecil Westerhof <Cecil@decebal.nl> - 2015-06-03 18:15 +0200
Re: Retrying to send message MRAB <python@mrabarnett.plus.com> - 2015-06-03 19:12 +0100
Re: Retrying to send message Ethan Furman <ethan@stoneleaf.us> - 2015-06-03 11:28 -0700
Re: Retrying to send message Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-06-03 21:37 +0100
Re: Retrying to send message Chris Angelico <rosuav@gmail.com> - 2015-06-04 08:00 +1000
Re: Retrying to send message Ethan Furman <ethan@stoneleaf.us> - 2015-06-03 16:15 -0700
Re: Retrying to send message Cecil Westerhof <Cecil@decebal.nl> - 2015-06-04 07:13 +0200
csiph-web