Handling an connection error with Twython

Cecil Westerhof Cecil at decebal.nl
Thu May 23 17:55:52 EDT 2019


Cecil Westerhof <Cecil at decebal.nl> writes:
> I am using Twython to post updates on Twitter. Lately there is now and
> then a problem with my internet connection. I am using:
> posted = twitter.update_status(status = message,
> in_reply_to_status_id = message_id,
> trim_user = True)
>> What would be the best way to catch a connection error and try it (for
> example) again maximum three times with a delay of one minute?

At the moment I solved it with the following:
 max_tries = 3
 current_try = 1
 while True:
 try:
 posted = twitter.update_status(status = message,
 in_reply_to_status_id = message_id,
 trim_user = True)
 return posted['id']
 except TwythonError as e:
 print('Failed on try: {0}'.format(current_try))
 if not 'Temporary failure in name resolution' in e.msg:
 raise
 if current_try == max_tries:
 raise
 current_try += 1
 time.sleep(60)
Is this a good way to do it, or can it be improved on?
When it goes OK I just return from the function.
If it goes wrong for something else as failure in the name resolution
I re-raise the exception.
When the maximum tries are done I re-raise the exception.
Otherwise I wait a minute to try it again.
-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /