Skip to content

How does fastapi establish Http long connection? #11604

Answered by YuriiMotov
zhao004 asked this question in Questions
Discussion options

You must be logged in to vote

Hi!

You can do it using StreamingResponse:

from fastapi import FastAPI
from fastapi.responses import StreamingResponse

app = FastAPI()


async def streamer():
    for i in range(10):
        yield b"some data"


@app.get("/")
async def main():
    return StreamingResponse(streamer())

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.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@zhao004
Comment options

Answer selected by zhao004
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
2 participants