title: Reduce latency in Asyncio
body: <p>I have an websocket server up and running fine using FastAPI.</p>
  <p>However, when i am using those &quot;await&quot;, i am getting some latency issues. At first, i though this had something to do with internet connection, or perhaps my linux server. But it appears to be that asyncio waits for other tasks.</p>
  <p>Here is my code:</p>
  <pre><code>import asyncio
  from pydantic import BaseModel
  
  class UserClientWebSocket(BaseModel):
      id: str
      ws: WebSocket
  
      class Config:
          arbitrary_types_allowed = True
  
  
  
  class ConnectionManager:
      def __init__(self):
  
          self.active_user_client_connections: List[UserClientWebSocket] = []
          self.collect_user_IDs = []
  
  
  
  
  async def connect_the_user_client(self, websocket: WebSocket, THE_USER_ID):
      await websocket.accept()
      await self.send_message_to_absolutely_everybody(f&quot;User: {THE_USER_ID} connected to server!&quot;)
      print(&quot;User: {} &quot;.format(THE_USER_ID) + &quot; Connected&quot;)
      if THE_USER_ID not in self.collect_user_IDs:
          self.collect_user_IDs.append(THE_USER_ID)
      else:
          await self.send_message_to_absolutely_everybody(f&quot;Somebody connected with the same ID as client: {THE_USER_ID}&quot;)
          await self.send_message_to_absolutely_everybody(&quot;but Vlori is a nice and kind guy, so he wil not get kicked :)&quot;)
          self.collect_user_IDs.append(THE_USER_ID)
      self.active_user_client_connections.append(UserClientWebSocket(ws=websocket, id=THE_USER_ID))
      await self.show_number_of_clients()
  
  
  async def function_one_send_message_to_absolutely_everybody(self, message: str):
      try:
          await asyncio.sleep(2)
          await asyncio.gather(*(conn.ws.send_text(message) for conn in self.active_webpage_client_connections))
          await asyncio.gather(*(conn.ws.send_text(message) for conn in self.active_user_client_connections))
      except:
          print(&quot;waiting&quot;)
  
  async def function_two_send_personal_message_to_user(self, message: str, websocket: WebSocket):
      try:
          await websocket.send_text(message)
      except:
          print(&quot;waiting for task..&quot;)
  </code></pre>
  <p>...
  ...
  ...
  ...
  ...</p>
  <p>and further down is the channel in which client connects:</p>
  <pre><code>@app.websocket(&quot;/ws/testchannel&quot;)
  async def websocket_endpoint(websocket: WebSocket):
      await websocket.accept()
  
      try:
          while True:
              data = await websocket.receive_text()
              print_result = print(&quot;Received data: {} &quot;.format(data))
  
              send_data_back_to_user = await websocket.send_text(f&quot;you sent message: {data}&quot;)
  
            
  
      except WebSocketDisconnect as e:
          print(&quot;client left chat, error = &quot;, e)
  </code></pre>
  <p>The code as it stands now works perfectly, and the performance is good! However, if i add an async def function under the &quot;send_data_back_to_user&quot; line such as this:</p>
  <pre><code>await connection_manager.function_one_send_message_to_absolutely_everybody(data)
  </code></pre>
  <p>Then there is a huge latency! why is that?</p>
  <p>I am playing around and tried this:</p>
  <pre><code>@app.websocket(&quot;/ws/testchannel&quot;)
  async def websocket_endpoint(websocket: WebSocket):
      await websocket.accept()
      try:
          while True:
              data = await websocket.receive_text()
              print_result = print(&quot;Received data: {} &quot;.format(data))
  
              send_data_back_to_user = await websocket.send_text(f&quot;you sent message: {data}&quot;)   # if i comment this line and the line underneath, and the speed is extremely fast!
  
     
              the_asyncio_loop = asyncio.get_event_loop()
  
              print_data_on_terminal = asyncio.gather(print_result)
              return_data_back_to_user = asyncio.gather(send_data_back_to_user)
              broadcast_msg = asyncio.gather(connection_manager.function_one_send_message_to_absolutely_everybody(data))
  
              run_all_loops_together = asyncio.gather(print_data_on_terminal, return_data_back_to_user, broadcast_msg)
  
              results = the_asyncio_loop.run_until_complete(run_all_loops_together)
  
              print(results)
  
  
      except WebSocketDisconnect as e:
          print(&quot;client left chat, error = &quot;, e)
  </code></pre>
  <p>but gives me the error:</p>
  <pre><code>TypeError: An asyncio.Future, a coroutine or an awaitable is required
  </code></pre>
  <p>could someone help me with this?</p>
  <p>thanks.</p>