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

相关推荐
曲幽6 小时前
FastAPI 身份验证总踩坑?这份 FastAPI Users “避坑指南”请收好
python·fastapi·web·jwt·oauth2·user·authentication
装不满的克莱因瓶6 小时前
掌握 RNN 与 LSTM 模型结构
人工智能·python·rnn·深度学习·神经网络·ai·lstm
jeffer_liu6 小时前
Spring AI 生产级实战:裁判员
java·人工智能·后端·spring·大模型
何以解忧,唯有..7 小时前
Python包管理工具pip:从入门到精通
开发语言·python·pip
金銀銅鐵7 小时前
用 Tkinter 实现简单的猜数字游戏
后端·python
copyer_xyf7 小时前
Python 模块与包的导入导出
前端·后端·python
ice8130331818 小时前
【Python】Matplotlib折线图绘制
开发语言·python·matplotlib
夜微凉48 小时前
三、Spring
java·后端·spring
copyer_xyf8 小时前
Python venv 虚拟环境
前端·后端·python
林爷万福9 小时前
GitHub 开源光谱数据处理项目推荐
python·光纤光谱仪