Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pause before retry connection in Python

I am trying to connect to a server. Sometimes I cannot reach the server and would like to pause for a few seconds before trying again. How would I implement the pause feature in Python. Here is what I have so far. Thank you.

   while True:
        try:
            response = urllib.request.urlopen(http)
        except URLError as e:
            continue
        break

I am using Python 3.2

like image 962
David Avatar asked May 01 '26 19:05

David


1 Answers

This will block the thread for 2 seconds before continuing:

import time
time.sleep(2)
like image 55
Ben Hoffstein Avatar answered May 03 '26 08:05

Ben Hoffstein