網站首頁 編程語言 正文
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
相關推薦
- 2022-10-02 react?redux及redux持久化示例詳解_React
- 2022-10-21 Golang驗證器之validator是使用詳解_Golang
- 2022-10-12 antd為Tree組件標題附加操作按鈕功能_Redis
- 2022-09-21 Flask深入了解Jinja2引擎的用法_python
- 2024-04-04 netty使用http和webSocket
- 2022-06-21 Oracle新增和刪除用戶_oracle
- 2022-11-04 深入了解Python中Lambda函數的用法_python
- 2022-12-09 Qt?自定義屬性Q_PROPERTY不顯示float類型的解決_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支