http通过StreamingHttpResponse完成连续的数据传输长链接方式
作者:Hayley-L 时间:2023-06-04 03:33:45
http通过StreamingHttpResponse完成连续的数据传输长链接
问题
http服务之间传递结果流
一个由flask封装起来的算法,一个由django封装的后台,我希望在django里通过requests调用flask的算法接口,flask可以分析一帧返回一帧结果,追求分析结果的实时返回,而不是完全分析完再完整返回结果
为了能完整返回结果,暂时想到的模式有以下三种:
一问一答:等待完整的分析结果,然后返回,最不济就用这种
我要你给(长链接):flask返回一个generator,django取next就得到下一个的结果
你有你给(理想,长链接):建立长链接,flask每分析出一帧结果,就返回
一次结果,直到分析结束,关闭连接
看到flask中有个flask_socketio建立socket连接,还没有实验
暂时用StreamingHttpResponse,generater能实现实时分析的感觉,属于第三种模式(你有你给)
django的StreamingHttpResponse可以返回generater,request调用返回generate的接口的时候,通过contextlib 的closing对流进行处理:
输出
#django 算法端,输出流
from django.http import StreamingHttpResponse
def stream_response(request):
def generate():
for i in range(10):
print(i)
yield 'hi ' + str(i)
print('sleep 3')
time.sleep(1)
return StreamingHttpResponse(generate(), )
#flask 算法端,输出流
@app.route('/re', methods=('POST', ))
def re():
@flask.stream_with_context
def generate():
for i in range(10):
print(i)
yield 'hi ' + str(i)
print('sleep 3')
time.sleep(1)
return flask.Response(generate())
输入
不区分flask,django,都可以通过request,contextlib 实现
#flask 算法端
@app.route('/test', methods=['POST', 'GET'])
def test():
url = 'http://172.16.68.151:8000/test2'
from contextlib import closing
with closing(requests.get(url, stream=True)) as r1:
for i in r1.iter_content():
print(i)
StreamingHttpResponse和HttpResponse
在修改以前的文件下载功能时,发现一个文件有5G,用HttpResponse实现时,服务器返回502错误,查看nginx log时,发现nginx log记录的是: upstream prematurely closed connection while reading response header from upstream。应该是nginx服务器从上游获取数据时超时了。
查了很多办法,修改了nginx的配置,但是仍然超时。
绝望之下,查了一下Django的文档,发现了StreamingHttpResponse,试了一下效率提高了很多。
后来仔细查了一下发现HttpResponse在使用文件迭代器时:
HttpResponse will consume the iterator immediately, store its content as a string, and discard it.
HttpResponse会直接使用迭代器对象,将迭代器对象的内容存储城字符串,然后返回给客户端,同时释放内存。可以当文件变大看出这是一个非常耗费时间和内存的过程。
而StreamingHttpResponse是将文件内容进行流式传输,
StreamingHttpResponse在官方文档的解释是:
The StreamingHttpResponse class is used to stream a response from Django to the browser. You might want to do this if generating the response takes too long or uses too much memory.
这是一种非常省时省内存的方法。但是因为StreamingHttpResponse的文件传输过程持续在整个response的过程中,所以这有可能会降低服务器的性能。
参考文档
来源:https://blog.csdn.net/weixin_33127753/article/details/86700114
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python爬虫程序中使用生产者与消费者模式时进程过早退出的问题
python基础中的文件对象详解
![](https://img.aspxhome.com/file/2023/1/109601_0s.png)
简单了解python单例模式的几种写法
golang 如何通过反射创建新对象
Python实现提取Excel指定关键词的行数据
![](https://img.aspxhome.com/file/2023/4/67834_0s.jpg)
如何实现python爬虫爬取视频时实现实时进度条显示
![](https://img.aspxhome.com/file/2023/4/78794_0s.png)
使用pyqt5 tablewidget 单元格设置正则表达式
![](https://img.aspxhome.com/file/2023/4/118544_0s.jpg)
mssql server .ldf和.mdf的文件附加数据库的sql语句
为2021年的第一场雪锦上添花:用matplotlib绘制雪花和雪景
![](https://img.aspxhome.com/file/2023/2/130832_0s.png)
浅谈PHP错误类型及屏蔽方法
Python中最好用的json库orjson用法详解
![](https://img.aspxhome.com/file/2023/9/112029_0s.png)
php+mysql开发的最简单在线题库(在线做题系统)完整案例
![](https://img.aspxhome.com/file/2023/3/553631_0s.png)
Javascript中实现trim()函数的两种方法
python下实现二叉堆以及堆排序的示例
php将12小时制转换成24小时制的方法
Vue.extend实现组件库message组件示例详解
![](https://img.aspxhome.com/file/2023/4/126614_0s.jpg)
python定时任务apscheduler的详细使用教程
PHP中substr_count()函数获取子字符串出现次数的方法
将django项目部署到centos的踩坑实战
Python+Pyecharts实现散点图的绘制
![](https://img.aspxhome.com/file/2023/1/92211_0s.png)