简体   繁体   中英

Python websocket with FastAPI does not return data immediately

I created a websocket connection with FastAPI to "stream" data which comes from a tedious calculation. The data chunks should be send as soon as they are available and not altogether at the very end. In the following code snipped shows my problem simplified. sleep() here shall simulate the calculation time between the resulting data.

from fastapi import FastAPI, WebSocket
import uvicorn
from time import sleep

app = FastAPI()

@app.websocket("/")
async def endpoint(websocket: WebSocket):
    await websocket.accept()
    while True:
        command = await websocket.receive_text()
        if command == "getData":
            data = {"name": "NAME1", "result": "ABC"}
            await websocket.send_json(data)
            sleep(3)
            data = {"name": "NAME2", "result": "DEF"}
            await websocket.send_json(data)
            sleep(3)
            data = {"name": "NAME3", "result": "GHI"}
            await websocket.send_json(data)

if __name__ == "__main__":
    uvicorn.run("api:app", host="127.0.0.1", port=8000, reload=True)

Problem is when executing this code the client receives no data for 6 seconds and afterwards all the data is transmitted at once. Why is this and how can I make the data chunks being send back one by one as soon as they are available?

time.sleep is a blocking operation, so even the event loop is suspended in the current thread.

Try using asyncio.sleep which is non-blocking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM