赞
踩
python 后端流式返回结果代码案例
- from fastapi import FastAPI
- from fastapi.responses import StreamingResponse
-
- app = FastAPI()
-
- @app.get("/items/{item_id}")
- async def read_item(item_id: int):
- async def stream_data():
- for i in range(100):
- yield f"data:{i}\n\n" # 使用yield发送数据
- return StreamingResponse(stream_data()) # 返回StreamingResponse对象
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。