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

相关推荐
草莓熊Lotso19 分钟前
Python 进阶核心:字典 / 文件操作 + 上下文管理器实战指南
数据结构·c++·人工智能·经验分享·笔记·git·python
alonewolf_9921 分钟前
深入Spring核心原理:从Bean生命周期到AOP动态代理全解析
java·后端·spring
天远Date Lab22 分钟前
Python实现用户消费潜力评估:天远个人消费能力等级API对接全攻略
java·大数据·网络·python
Justin3go7 小时前
HUNT0 上线了——尽早发布,尽早发现
前端·后端·程序员
Tony Bai7 小时前
高并发后端:坚守 Go,还是拥抱 Rust?
开发语言·后端·golang·rust
一线大码8 小时前
SpringBoot 3 和 4 的版本新特性和升级要点
java·spring boot·后端
秃了也弱了。8 小时前
python实现定时任务:schedule库、APScheduler库
开发语言·python
Dfreedom.8 小时前
从 model(x) 到__call__:解密深度学习框架的设计基石
人工智能·pytorch·python·深度学习·call
weixin_425023008 小时前
Spring Boot 配置文件优先级详解
spring boot·后端·python
weixin_425023008 小时前
Spring Boot 实用核心技巧汇总:日期格式化、线程管控、MCP服务、AOP进阶等
java·spring boot·后端