flask实现Streaming内容传输

当传输大量内存,以至于超出内存大小,一般http服务器会报500错误,这时可以使用Streaming流的方式来传输内容,类似ChatGPT和视频流那样的输出方式,flask里要用到生成器和直接响应。

python 复制代码
from flask import stream_with_context, request

@app.route('/stream')
def streamed_response():
    def generate():
        yield 'Hello '
        yield request.args['name']
        yield '!'
    return app.response_class(stream_with_context(generate()))

相关链接

https://www.osgeo.cn/flask/patterns/streaming.html

相关推荐
独泪了无痕7 小时前
MyBatis魔法堂:结果集映射
后端·mybatis
大貔貅喝啤酒7 小时前
Python Requests库教程
自动化测试·python·requests库
copyer_xyf7 小时前
LangChain 调用 LLM
后端·python·agent
copyer_xyf7 小时前
Prompt 组织管理
后端·python·agent
shimly1234568 小时前
python3 uvicorn 是啥?
python
CTA量化套保9 小时前
期货量化程序 time.sleep 卡死:天勤单线程与 deadline 替代
python·区块链
摇滚侠9 小时前
SpringMVC 入门到实战 文件上传 75-77
java·后端·spring·maven·intellij-idea
GIS数据转换器9 小时前
城市排水生命线安全运行监测平台深度解析
java·运维·人工智能·python·安全·数据挖掘·无人机
贤哥哥yyds10 小时前
GBK转UTF\-8编码自动转换工具 使用文档
python
数量技术宅10 小时前
2026量化前沿:从Reddit热帖到Python实战,如何用赫斯特指数(Hurst)狙击虚假突破?
开发语言·python