title: Python Websockets : infinite lock on client recv
body: <p>I am learning how to use the <code>websockets</code> package for python 3.6 with <code>asyncio</code>.</p>
  
  <p>Using the <a href="https://websockets.readthedocs.io/en/stable/intro.html" rel="nofollow noreferrer">Websockets Getting Started</a> example, here are my server and client code (both running in two separate console using <code>python &lt;script&gt;</code>)</p>
  
  <p>wsserver.py</p>
  
  <pre><code>import asyncio
  
  import websockets
  
  msg_queue = asyncio.Queue()
  
  
  async def consumer_handler(websocket):
      global msg_queue
      while True:
          message = await websocket.recv()
          print("Received message {}".format(message))
          await msg_queue.put("Hello {}".format(message))
          print("Message queued")
  
  
  async def producer_handler(websocket):
      global msg_queue
      while True:
          print("Waiting for message in queue")
          message = await msg_queue.get()
          print("Poped message {}".format(message))
          websocket.send(message)
          print("Message '{}' sent".format(message))
  
  
  async def handler(websocket, path):
      print("Got a new connection...")
      consumer_task = asyncio.ensure_future(consumer_handler(websocket))
      producer_task = asyncio.ensure_future(producer_handler(websocket))
  
      done, pending = await asyncio.wait([consumer_task, producer_task]
                                         , return_when=asyncio.FIRST_COMPLETED)
      print("Connection closed, canceling pending tasks")
      for task in pending:
          task.cancel()
  
  
  start_server = websockets.serve(handler, 'localhost', 5555)
  
  asyncio.get_event_loop().run_until_complete(start_server)
  asyncio.get_event_loop().run_forever()
  </code></pre>
  
  <hr>
  
  <p>wsclient.py</p>
  
  <pre><code>import asyncio
  import websockets
  
  async def repl():
      async with websockets.connect('ws://localhost:5555') as websocket:
          while True:
              name = input("\nWhat's your name? ")
  
              await websocket.send(name)
              print("Message sent! Waiting for server answer")
  
              greeting = await websocket.recv()
              # never goes here
              print("&gt; {}".format(greeting))
  
  asyncio.get_event_loop().run_until_complete(repl())
  </code></pre>
  
  <hr>
  
  <p>During the execution, the server is doing what is expected of him : </p>
  
  <ul>
  <li>Wait for a client message</li>
  <li>Queue <code>'Hello $message'</code></li>
  <li>Dequeue it</li>
  <li>Send the dequeued message back to the sender</li>
  </ul>
  
  <p>The client does work up to the waiting of the server response : </p>
  
  <ul>
  <li>Wait for a user input</li>
  <li>Send it to the server</li>
  <li>Wait answer from the server  &lt;-- Holds on indefinitely</li>
  <li>Print it &amp; loop</li>
  </ul>
  
  <p>Here are the console outputs of the execution : </p>
  
  <p>Server</p>
  
  <pre><code>Got a new connection...
  Waiting for message in queue
  Received message TestName
  Message queued
  Poped message Hello TestName 
  Message 'Hello TestName' sent 
  Waiting for message in queue
  </code></pre>
  
  <p>Client</p>
  
  <pre><code>What's your name? TestName
  Message sent! Waiting for server answer
  _
  </code></pre>
  
  <p>What am I missing?</p>