title: Is it possible to send parallel requests from one Websocket connection?
body: <p>I'm actually using <a href="https://websockets.readthedocs.io/en/stable/" rel="nofollow noreferrer">WebSockets 7</a> and everything goes fine, multiple users can connect to the websocket, send requests and receive responses from server, but there is a <em>problem</em>.</p>
  
  <p>If a user send to image, pdf, song, video, etc. Server must resolve that, saving to db, converting, etc, and sending response, the user could send more data to websocket but that data will be queued until previous message got response, that is not good, when users are desperate to send data in shortest time possible.</p>
  
  <p>There is a possibility for a user connected to send <em>parallel requests</em>?</p>
  
  <p>This is the example I'm using</p>
  
  <pre><code>import asyncio
  import websockets
  
  # Process the data
  async def read(msg, websocket):
      # In this case, the message will get back to the user
      await websocket.send(msg)
  
  async def counter(websocket, path):
      try:
          async for message in websocket:
              await read(message, websocket)
      except websockets.exceptions.ConnectionClosed:
          pass
  
  try:
      asyncio.get_event_loop().run_until_complete(
          websockets.serve(counter, '0.0.0.0', 4444, max_size=10**8))
      asyncio.get_event_loop().run_forever()
  except KeyboardInterrupt:
      quit()
  </code></pre>
  
  <h2>Update</h2>
  
  <p>I'm using the loop, in the trying to make a request in parallel, it does not work though.</p>
  
  <pre><code>import asyncio
  import websockets
  
  # Process the data
  async def read(msg, websocket):
      # In this case, the message will get back to the user
      if msg == 'long':
          # Some blocking operations
          pass
      elif msg == 'short':
          # Some blocking operations
          pass
      # elif msg == 'some other conditions that could take more or less time':
          # Some blocking operations
          # pass
      # elif msg == 'some other conditions that could take more or less time':
          # Some blocking operations
          # pass
      # elif msg == 'some other conditions that could take more or less time':
          # Some blocking operations
          # pass
      # elif msg == 'some other conditions that could take more or less time':
          # Some blocking operations
         # pass
  
      print("Did a {0} task".format(msg))
      await websocket.send(msg)
  
  async def counter(websocket, path):
      loop = asyncio.get_event_loop()
      try:
          async for message in websocket:
              loop.create_task(read(message, websocket))
      except websockets.exceptions.ConnectionClosed:
          pass
  
  try:
      asyncio.get_event_loop().run_until_complete(
          websockets.serve(counter, '0.0.0.0', 4444, max_size=10**8))
      asyncio.get_event_loop().run_forever()
  except KeyboardInterrupt:
      quit()
  </code></pre>
  
  <p>When user send 'long', and then send 'short', "long" will make "short" wait until it is processed. :(</p>
  
  <p>If add <code>await asyncio.sleep()</code> inside every condition, it will take parallelously but longest task must have longest time for <code>asyncio.sleep()</code>
  eg:</p>
  
  <pre><code>    if msg == 'this is probably the longest task to do':
          await asyncio.sleep(5);
          # Some blocking operations
      elif msg == 'this is probably the shortest task to do':
          await asyncio.sleep(1);
          # Some blocking operations
      elif msg == 'some other conditions that could take more or less time':
          await asyncio.sleep(4);
          # Some blocking operations
      elif msg == 'some other conditions that could take more or less time':
          await asyncio.sleep(2);
          # Some blocking operations
      elif msg == 'some other conditions that could take more or less time':
          await asyncio.sleep(3);
          # Some blocking operations
      elif msg == 'some other conditions that could take more or less time':
          await asyncio.sleep(1);
          # Some blocking operations
  </code></pre>
  
  <p>If I remove <code>await asyncio.sleep()</code>, code will run non-parallelously</p>