title: How to effectively multi-thread functions based on data received from a socket in python?
body: <p>Here is a snippet from a specific server program I a working on. </p>
  
  <p>Requirements: </p>
  
  <ol>
  <li>I want the program to initiate a socket on port defined by <code>ownport</code> variable and listen on that port for incoming TCP messages. </li>
  <li>Based on the specific value I receive on the socket from remote hosts, the server has to initiate functions <code>func1()</code> or <code>func2()</code> [Example: if the value is <code>JOIN</code>, then execute <code>func1</code> and if it is <code>UPDATE</code> execute <code>func2</code>]</li>
  <li>These actions are mutually exclusive and multiple clients may be connected to the server at the same time and might be sending requests. </li>
  </ol>
  
  <p>How can I effectively multithread this so that the server constantly listen on the port and initiate the functions based on the incoming requests in realtime? </p>
  
  <pre><code># Creating a socket 
  try:
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  except socket.error, msg:
      print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
      sys.exit();
  print "Socket created!"
  
  #Binding the socket to specified ports
  try:
      sock.bind((ownhost, ownport))
  except socket.error , msg:
      print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
      sys.exit()
  print 'Socket bind complete'
  
  # Listening for incoming requests
  sock.listen(10)
  print 'Socket now listening'
  conn, addr = sock.accept()
  print 'Connected with ' + addr[0] + ':' + str(addr[1])
  
  # Handling the incoming requests received
  req = conn.recv(1024)
  reqpro = req.split('|')
  # If the request is a join request
  if reqpro[0] == "JOIN":
      func1(reqpro, arg2)
  elif (reqpro[0] == 'UPDATE') and (reqpro[1] == 'PRED'):
      func2(reqpro,arg2)
  else:
      print "invalid request type"
      sys.exit()
  </code></pre>