title: How do I manage TCP Client read/write overlap issues?
body: <p>I have a TCP client communicating with a LabVIEW GUI. </p>
  
  <p>My program calls <code>connect()</code> at the start and <code>disconnect()</code> at the end. It will call <code>passCommand(x)</code> to read or write data to the LabVIEW GUI. However, in some cases, I have multiple threads which may be calling <code>passCommand()</code> and somehow the return data will get mixed up.</p>
  
  <p>For example, in the main thread I will ask for the voltage, which should be a number between 300 and 400. In a different thread I will ask for the temperature, which should be a number from 0-100. The voltage will be returned as 25, while the temperature will get 250. </p>
  
  <p>Is this a known issue with TCP communication and threading? Is there a way to solve this such as implementing a queue or unique id or something?</p>
  
  <pre><code>import socket as _socket
  
  # get python major version as integer
  from sys import version as pythonVersion
  pythonVersionMajor = int(pythonVersion[0])
  
  _serverHost = 'localhost'
  _serverPort = 50007
  isConnected = 0
  _sockobj = None
  _error_string = "error:"
  
  
  def connect():
      'opens a connection to LabVIEW Server'
      global _sockobj, isConnected
      _sockobj = _socket.socket(_socket.AF_INET, _socket.SOCK_STREAM)      # create socket
      _sockobj.connect((_serverHost, _serverPort))   # connect to LV
      isConnected = 1
  
  
  def disconnect():
      'closes the connection to LabVIEW Server'
      global isConnected
      _sockobj.close()                             # close socket
      isConnected = 0
  
  
  def passCommand(command):
      'passes a command to LabVIEW Server'
  
      ## We prepend the command length (8 char long) to the message and send it to LV
      # Compute message length and pad with 0 on the left if required
      commandSize=str(len(command)).rjust(8,'0')
      # Prepend msg size to msg
      completeCommand=commandSize+command
      # python 3 requires data to be encoded
      if (pythonVersionMajor &gt;= 3):
          completeCommand = str.encode(completeCommand)
      # Send complete command
      _sockobj.send(completeCommand)
      data = _sockobj.recv(11565536)
      # python 3 requires data to be decoded
      if (pythonVersionMajor &gt;= 3):
          data = bytes.decode(data)
      if data.rfind(_error_string) == 0:
          error = True
          data = data[len(_error_string):] # get data after "error:" string
      else:
          error = False
      execString = "lvdata = " + data
      exec(execString, globals())
      if error:
          raise _LabVIEWError(lvdata)
      else:
          return lvdata
  
  
  class _Error(Exception):
      """Base class for exceptions in this module."""
      pass
  
  
  class _LabVIEWError(_Error):
      """Exception raised for errors generated in LabVIEW.
  
      Attributes:
          code -- LabVIEW Error Code
          source -- location of the error
          message -- explanation of the error
      """
  
      def __init__(self, error):
          self.code = error[0]
          self.source = error[1]        
          self.message = error[2]
  
      def __str__(self):
          return "%s" % (self.message,)
  </code></pre>