How does fastapi establish Http long connection? #11604
-
First Check
Commit to Help
Example Codefrom fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
# How to write Http long connection API? DescriptionHow to usefastapi to build a long HTTP connection and return data bit by bit like in the picture?Operating SystemWindows Operating System Detailswindows10 FastAPI Versionfastapi 0.111.0 Pydantic Versionpydantic 2.7.1 Python VersionPython 3.11.1 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Answered by
YuriiMotov
May 20, 2024
Replies: 1 comment 1 reply
-
Hi! You can do it using
In the example above you need to change |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
zhao004
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!
You can do it using
StreamingResponse
:In the example above you need to change
streamer
function to wait for the portion of data to send, send it (yield data_as_bytes
) and wait for the next portion of data.