The change I made was to override the handle_error method in asyncore above to simply call another method to try to initialize the connection again instead of closing the socket. As in below: (following code is added to DETClient above)
def initiate_connection_with_server(self):
print("trying to initialize connection with server...")
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect((self.host,self.port))
def handle_error(self):
print("problem reaching server.")
self.initiate_connection_with_server()
This solves the problem of the server not being available when this code is run. An exception is raised and handle_error is called which simply calls the initiate_connection method and tries to open the socket again. Also, after the connection is initially established, the code will call handle_error if the socket is lost for any reason and an attempt will be made to re-establish the connection. Problem solved!
Here is the code for the threaded module (ReceiverBoard)
class ReceiverBoard(threading.Thread):
_stop = False
def __init__(self, client):
self.client=client
super(ReceiverBoard,self).__init__()
def run(self):
while True:
block_to_send = ""
##code here to generate block_to_send
self.client.sendCommand(block_to_send)
def stop(self):
self._stop = True