Reduce latency in Asyncio

1.2k Views Asked by At

I have an websocket server up and running fine using FastAPI.

However, when i am using those "await", 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.

Here is my 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"User: {THE_USER_ID} connected to server!")
    print("User: {} ".format(THE_USER_ID) + " Connected")
    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"Somebody connected with the same ID as client: {THE_USER_ID}")
        await self.send_message_to_absolutely_everybody("but Vlori is a nice and kind guy, so he wil not get kicked :)")
        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("waiting")

async def function_two_send_personal_message_to_user(self, message: str, websocket: WebSocket):
    try:
        await websocket.send_text(message)
    except:
        print("waiting for task..")

... ... ... ... ...

and further down is the channel in which client connects:

@app.websocket("/ws/testchannel")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()

    try:
        while True:
            data = await websocket.receive_text()
            print_result = print("Received data: {} ".format(data))

            send_data_back_to_user = await websocket.send_text(f"you sent message: {data}")

          

    except WebSocketDisconnect as e:
        print("client left chat, error = ", e)

The code as it stands now works perfectly, and the performance is good! However, if i add an async def function under the "send_data_back_to_user" line such as this:

await connection_manager.function_one_send_message_to_absolutely_everybody(data)

Then there is a huge latency! why is that?

I am playing around and tried this:

@app.websocket("/ws/testchannel")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    try:
        while True:
            data = await websocket.receive_text()
            print_result = print("Received data: {} ".format(data))

            send_data_back_to_user = await websocket.send_text(f"you sent message: {data}")   # 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("client left chat, error = ", e)

but gives me the error:

TypeError: An asyncio.Future, a coroutine or an awaitable is required

could someone help me with this?

thanks.

0

There are 0 best solutions below