title: Receive in two functions from one Socket
body: <p>I want to create a Socket Client and want to receive the answer if a message is sent in one function but also want to listen to all incoming data all the time.</p>
  <p>The problem is, if I do both, the code stops executing as <code>recv()</code> seems to be a blocking function.</p>
  <p>This is a quick overview:</p>
  <p><strong>Server.py</strong></p>
  <pre><code>import socket
  
  HOST = &quot;127.0.0.1&quot;
  PORT = 6543
  
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.bind((HOST, PORT))
  s.listen()
  
  conn, addr = s.accept()
  
  with conn:
      print(&quot;Connected by &quot;, addr)
      while True:
          data = conn.recv(1024)
          conn.sendall(data)
  </code></pre>
  <p><strong>Client.py</strong></p>
  <pre><code>import socket
  import time
  import multiprocessing
  
  HOST = &quot;127.0.0.1&quot;
  PORT = 6543
  
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
  s.connect((HOST, PORT))
  
  def keepAlive():
      while True:
          s.send(b&quot;Keep Alive to be handled in keepAlive&quot;)
          print(s.recv(1024), &quot;received from keepAlive&quot;)
          time.sleep(1)
  
  def send():
      while True:
          s.send(b&quot;Random message to be handled in listen&quot;)
          time.sleep(1)
  
  
  def listen():
      while True:
          print(s.recv(1024), &quot;received from listen&quot;)
  
  if __name__ == &quot;__main__&quot;:
      keepAliveProc = multiprocessing.Process(target=keepAlive)
      listenProc = multiprocessing.Process(target=listen)
      sendProc = multiprocessing.Process(target=send)
  
      keepAliveProc.start()
      listenProc.start()
      sendProc.start()
  
  </code></pre>
  <p>But this will block and the Code doesn't continue. Can anyone help with this?</p>