StreamingResponse is returning all content at once #10701
-
First Check
Commit to Help
Example Codefrom fastapi import FastAPI
from fastapi.responses import StreamingResponse
import json
import time
app = FastAPI()
async 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_streamer(), media_type='text/event-stream') DescriptionI'm using That is, when I access the endpoint with the following client code: import requests
import time
t0 = time.time()
response = requests.get('http://localhost:8080/test')
for chunk in response.iter_lines():
print(f"Chunk (time {int((time.time()-t0)*1000)}ms): {chunk}") I see the output (in the client):
And on the server logs I see:
That is, the chuncks of json content are being generated and yielded roughly every 500ms, but the client only see the the result at 5026ms. If this is a problem with the client code ( Operating SystemLinux Operating System DetailsUbuntu 20.04.6 LTS FastAPI Version0.99.1 Pydantic Version1.10.13 Python Version3.8.10 Additional ContextThis is a simplified example that I hope it's easy to reproduce. |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 13 replies
-
Hello, You can also check it with curl, for example Do note that your code is not using |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Weird... Are you using the same version as me? |
Beta Was this translation helpful? Give feedback.
-
Missing |
Beta Was this translation helpful? Give feedback.
-
I'm sorry @Kludex, but I still see the same problem: Is it possible that the problem is not related to FastAPI? |
Beta Was this translation helpful? Give feedback.
-
Compression middleware leads to such a pattern. If that's the case, try removing it from the streaming endpoint and setting it per each router. |
Beta Was this translation helpful? Give feedback.
-
@LeonardoSanBenitez are you running through some proxy? if yes that's probably the reason, some proxies don't proxies SSE properly! |
Beta Was this translation helpful? Give feedback.
-
Ever find a solution here? I am running into the same problem. I think it may be related to the use of an async generator as the function the route is streaming. Although I am unable to find a solution. |
Beta Was this translation helpful? Give feedback.
-
I have met the same problem and find the real reason of my case. Because I have a Gzip Middleware, this middleware cause the stream response not work. When I disable Gzip middleware, everything works well. |
Beta Was this translation helpful? Give feedback.
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