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

相关推荐
leeyi4 小时前
命令行里的平台:df_cli 都会什么(第108篇)
后端·aigc·agent
2601_962295584 小时前
Python UI自动化测试用例脚本编写指南
python·测试用例·uiautomator2·ui自动化测试·脚本编写
云雀衔光4 小时前
Java Spring AI MCP:在 Spring 里把 AI 接上你的业务系统
java·开发语言·人工智能·后端·测试工具·spring
重生之小比特4 小时前
【Java SE】数据类型与变量
java·开发语言·python
苏渡苇4 小时前
Spring Insight 里如何对 Span 进行清洗
java·spring boot·后端·spring·系统监控
LadenKiller5 小时前
先分清阶段,再让工具帮量化学习
人工智能·python
WeiXin_DZbishe5 小时前
基于springboot大学生提问箱系统-计算机毕设【课程设计】72593
javascript·vue.js·spring boot·vscode·python·node.js·php
jsjzsl25 小时前
独立自由度框架下位置自由度的本体论特征、物理现象与新技术路径
python·flask·fastapi
z_mazin5 小时前
逆向一种私有二进制序列化格式:从零到字节级解析器
网络·python·网络协议·算法·rpc
Moment5 小时前
为什么市面上的 coding agent 大多数都基于Nodejs?
前端·后端·面试