flask流式接口

一、接口封装

复制代码
from flask import Flask, request, Response, stream_with_context
app = Flask(__name__)
app.logger.disabled = True

def chat_stream_(prompt):
    for new_text in ['1','2','3']:
        yield new_text

@app.route('/chat_stream', methods=['POST'])
def chat_stream():
    prompt = request.json['prompt']
    return app.response_class(stream_with_context(chat_stream_(prompt)))


if __name__ == '__main__':
    app.run('0.0.0.0', 1000)

二、接收流式返回

复制代码
import requests
url = "http://127.0.0.1:1000/chat_stream"
data = {"prompt":"怎么编研文档"}
res = requests.post(url, json=data, stream=True)
for token in res:
    print(token.decode("utf-8"))
相关推荐
坐吃山猪18 分钟前
Python进度条
linux·服务器·python
why技术20 分钟前
我拿到了腾讯QClaw的内测码,然后沉默了。
前端·后端
l1t38 分钟前
四种python工具包用SQL查询csv和parquet文件的方法比较
大数据·python·sql
清水白石00839 分钟前
Python 并发三剑客:多线程、多进程与协程的实战抉择
java·服务器·python
2301_7938046939 分钟前
更优雅的测试:Pytest框架入门
jvm·数据库·python
dinl_vin1 小时前
python:常用的基础工具包
开发语言·python
只能是遇见1 小时前
SpringBoot + vue 管理系统
vue.js·spring boot·后端
wefly20171 小时前
无需安装、开箱即用!m3u8live.cn 在线 HLS 播放器,调试直播流效率翻倍
前端·后端·python·前端开发工具·后端开发工具
2301_815482932 小时前
用Python实现自动化的Web测试(Selenium)
jvm·数据库·python
IT_陈寒2 小时前
React组件性能翻倍的5个冷门技巧,90%的开发者不知道!
前端·人工智能·后端