uvicorn launching fastapi Streaming works fine when using reload and not when not using it #11565
-
First Check
Commit to Help
Example Code# main.py
from fastapi import FastAPI, Request
from fastapi.responses import StreamingResponse
import time
app = FastAPI()
async def generate_data():
for i in range(10):
time.sleep(0.1)
yield f"data {i}\n"
@app.post("/test")
async def agent_chain(request: Request):
form_data = await request.form()
res = generate_data()
async def generate():
async for i in res:
print(f"stream data:{i}")
yield i
for i in form_data.get("data"):
yield f"data {i}\n"
return StreamingResponse(generate(), media_type="text/event-stream")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=12334)
#request.py
import requests
data = {"data": "hello world"}
response = requests.post("http://localhost:12334/test", data=data, stream=True)
for chunk in response.iter_content(chunk_size=1024):
if chunk:
print(chunk) DescriptionWhen I use fastapi to make a streaming interface, I encountered such a problem, not quite sure whether it is a problem with the code I wrote or a problem with fastapi, so I mentioned here to start the question. uvicorn main:app --port 12334 --reload But when I use the following command, the streaming doesn't return properly and the returned response is returned as if it were non-streaming. uvicorn main:app --port 12334 My library version
I want to know if I need to stream normally without adding --reload how should I implement it. 2024-05-10.16-23-08.mp4Operating SystemLinux, Windows Operating System DetailsNo response FastAPI Version0.111.0 Pydantic Version2.7.0b1 Python Version3.10.14 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You only need to await asyncio.sleep(0.05) |
Beta Was this translation helpful? Give feedback.
You only need to await asyncio.sleep(0.05)