I wrote a code in python 3.9.0 which opens a text file, read line by line and host it at web socket for clients to consume. It works just fine. The problem is, when I consume the file from the web socket server it is really slow. I can see that server sends it faster in thousands but client is receiving it slow. I am not sure what am I doing wrong with my client code. Here is my client code
async def consumeStream():
while True:
async with websockets.connect('ws://localhost:6800') as ws:
ingestCounter = 0
while True:
try:
consumedStream = await ws.recv()
except websockets.ConnectionClosed:
print(color.red+'Websocket connection closed, retrying !'+color.reset)
break
if ingestCounter % 100 == 0:
print('\r'+f'Consuming: {ingestCounter}', end='')
ingestCounter += 1
await asyncio.sleep(0)
Is streaming good idea with web sockets? Should I use Flask or FastAPI with yield ? What is the best practice for web sockets streaming and consuming ?