python socket recv in multithread
<p>I have python script with only one socket object that is connect to a java server.</p>

<p>I started a thread for sending heart beat message to server per 5 secs.</p>

<p>And another thread for receiving message from server.</p>

<p>BTW, all the data send/recv is in protobuffer format.</p>

<pre><code># socket_client.py

def recv_handler():
    global client_socket
    while True:
        try:
            # read 4 bytes first 
            pack_len = client_socket.recv(4)
            pack_len = struct.unpack('!i', pack_len)[0]
            # read the rest
            recv_data = client_socket.recv(pack_len)
            # decode
            decompressed_data = data_util.decompressMessage(recv_data)
            sc_pb_message = data_util.decodePBMessage(decompressed_data)
            sc_head = data_util.parseHead(sc_pb_message)
        except:
            print 'error'

def heart_handler():
    global client_socket
    while True:
        if client_socket:
            message = data_util.makeMessage('MSG_HEART_BEAT')
            compressed_data = data_util.compressMessage(message)
            send_data = data_util.makeSendData(compressed_data)
            try: 
                client_socket.send(send_data)
            except: 
                print 'except'
                pass
        time.sleep(5)

def connect(address, port):
    global client_socket
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((address, port))

    # thread sending heart beat message
    th = threading.Thread(target = heart_handler)
    th.start()

    # thread recving message
    tr = threading.Thread(target = recv_handler)
    tr.start()
</code></pre>

<p>The code above works just fine. The script will send a heart beat message per 5 secs and receive the message from server and the message can be decoded successfully.</p>

<p>And here comes the trigger part than I do not know how to implement.</p>

<p>My python script need to receive input from the browser at the same time, so I started a BaseHTTPServer, to handle the POST request from the browser.</p>

<p>When a request come, I would like to call the client_socket.send method to send a specific message to the server and of course I need to return the data from server back to the browser.</p>

<pre><code># http_server.py

def do_POST(self):
    # ...
    result = socket_client.request(message)
    self.send_response(200)
    self.end_headers()
    self.wfile.write(...)
</code></pre>

<p>And here is what I tried to do in request:</p>

<pre><code>def request(message):
    global client_socket
    client_socket.send(message)
    pack_len = client_socket.recv(4)
    pack_len = struct.unpack('!i', pack_len)[0]
    recv_data = client_socket.recv(pack_len)
    return recv_data
</code></pre>

<p>The problem I am having is the data I received in the request method after calling the send method seems to be disturbed by the data of heart beat in the thread.</p>

<p>If I comment out the heart beat thread and the receive thread, than the request method will work just fine. The data from server can decoded with no error and it can be sent back to the browser successfully.</p>

<p>My solution now might be wrong and I really do not know how to get this work.</p>

<p>Any advice will be appreciated, thanks :)</p>