Handling an connection error with Twython

Cecil Westerhof Cecil at decebal.nl
Sat Jun 1 15:12:28 EDT 2019


"Peter J. Holzer" <hjp-python at hjp.at> writes:

> On 2019-05-25 13:46:40 +0200, Cecil Westerhof wrote:
>> Just changing the while loop to a for loop did not make sense to me,
>> but this does. I now have:
>>     max_tries = 5
>>     for current_try in range(1, max_tries):
>>         try:
>>             posted = twitter.update_status(status = message,
>>                                            in_reply_to_status_id = message_id,
>>                                            trim_user = True)
>>             return posted['id']
>>         except TwythonError as e:
>>             if not 'Temporary failure in name resolution' in e.msg:
>>                 raise
>>             timed_message('Failed on try: {0} of {1}'.format(current_try, max_tries))
>>             if current_try == max_tries:
>                  ^^^^^^^^^^^^^^^^^^^^^^^^
>                  This will never be true because the loop will exit
>                  before current_try reaches max_tries.
>>                 print('Could not get a connection to the internet: exiting')
>>                 deinit(2)
>                 Also you don't seem to stop here (unless deinit raises
>                 an exception)
>
>>             time.sleep(60)
>>     raise RuntimeError('Should not get here')
>                          ^^^^^^^^^^^^^^^^^^^^^
>       So if you can't get a status update you will reach this point.

Oops, you are completely right. :'-( The for should be:
    for current_try in range(1, max_tries + 1):

A bit silly of me.


The function deinit does some deinitialisation and after that an exit
with the value it receives. (Default it uses 0.) So you never return
from this call. Should have made that clear.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof



More information about the Python-list mailing list