Retrying to send message
Cecil Westerhof
Cecil at decebal.nl
Wed Jun 3 08:27:52 EDT 2015
I am using libturpial to post messages on Twitter. Sometimes I get a
libturpial.exceptions.ServiceOverCapacity.
It is a good idea to try it again then. For this I wrote the following
function:
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. ;-)
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?
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
More information about the Python-list
mailing list