title: Listening for socket input in multiple threads
body: <p>I'm using python sockets.</p>
  
  <p>Here's the problem. I've 2 threads:</p>
  
  <ul>
  <li>One thread listens for socket input from remote and reply to it</li>
  <li>One thread polls file and if something is present in file then send
  to socket and expect a response.</li>
  </ul>
  
  <p>Now the problem is in case of second thread when I send something, the response doesn't come to this thread. Rather it comes to thread mentioned in (1) point.</p>
  
  <p>This is thread (1)
  <code></p>
  
  <pre><code>def client_handler(client):
      global client_name_to_sock_mapping
      client.send(first_response + server_name[:-1] + ", Press ^C to exit")
      user_name = None
      while True:
          request = client.recv(RECV_BUFFER_LIMIT)
          if not user_name:
              user_name = process_input(client, request.decode('utf-8'))
              user_name = user_name.rstrip()
              if user_name not in client_name_to_sock_mapping.keys():
                  client_name_to_sock_mapping[user_name] = client
              else:
                  msg = "Username not available".encode('ascii')
                  client.send(msg)
          else:
              process_input(client, request.decode('utf-8'), user_name)
  </code></pre>
  
  <p></code></p>
  
  <p>This is run from thread (2)
  <code></p>
  
  <pre><code>def send_compute_to_client():
      time.sleep(20)
      print("Sleep over")
      for _, client_sock in client_name_to_sock_mapping.iteritems():
          print("client = {}".format(client_sock))
          client_sock.sendall("COMPUTE 1,2,3")
          print("send completed = {}".format(client_sock))
          data = client_sock.recv(1024)
          print("Computed results from client {}".format(data))
  </code></pre>
  
  <p></code>
  Can someone please explain this behaviour?</p>