Skip to content

StreamingResponse is returning all content at once #10701

Discussion options

You must be logged in to vote

hi @LeonardoSanBenitez hope you have resolved the issue, i faced same issue currently and was able to solve it by using non asynchronous generator function, so in your case

from fastapi import FastAPI
from fastapi.responses import StreamingResponse

import json
import time
import asyncio

app = FastAPI()

def fake_json_streamer():
    t0 = time.time()
    for i in range(10):
        print(f"Chunk being yielded (time {int((time.time()-t0)*1000)}ms)", flush=True)
        yield json.dumps( {"message": "Hello World"}) + '\n'
        time.sleep(0.5)
    print(f"Over (time {int((time.time()-t0)*1000)}ms)", flush=True)

@app.get("/test")
async def test():
    return StreamingResponse(fake_json_s…

Replies: 9 comments 13 replies

Comment options

You must be logged in to vote
0 replies
Comment options

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

Comment options

You must be logged in to vote
0 replies
Comment options

Kludex
Nov 23, 2023
Collaborator Sponsor

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

Comment options

You must be logged in to vote
4 replies
@Kludex
Comment options

Kludex Nov 24, 2023
Collaborator Sponsor

@LeonardoSanBenitez
Comment options

@anantgupta129
Comment options

Answer selected by LeonardoSanBenitez
@anantgupta129
Comment options

Comment options

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

Comment options

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

Comment options

You must be logged in to vote
2 replies
@lyris
Comment options

@LeonardoSanBenitez
Comment options

Comment options

You must be logged in to vote
3 replies
@La-fe
Comment options

@anantgupta129
Comment options

@idailylife
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem