Flask實(shí)現(xiàn)異步非阻塞請求功能實(shí)例解析
本文研究的主要是Flask實(shí)現(xiàn)異步非阻塞請求功能,具體實(shí)現(xiàn)如下。
最近做物聯(lián)網(wǎng)項(xiàng)目的時(shí)候需要搭建一個(gè)異步非阻塞的HTTP服務(wù)器,經(jīng)過查找資料,發(fā)現(xiàn)可以使用gevent包。
關(guān)于gevent
Gevent 是一個(gè) Python 并發(fā)網(wǎng)絡(luò)庫,它使用了基于 libevent 事件循環(huán)的 greenlet 來提供一個(gè)高級同步 API。下面是代碼示例:
from gevent.wsgi import WSGIServer from yourapplication import app http_server = WSGIServer(('', 5000), app) http_server.serve_forever()
代碼清單
下面放上Flask異步非阻塞的代碼清單,以后需要用到的時(shí)候直接移植即可。
# coding=utf-8 # Python Version: 3.5.1 # Flask from flask import Flask, request, g # gevent from gevent import monkey from gevent.pywsgi import WSGIServer monkey.patch_all() # gevent end import time app = Flask(__name__) app.config.update(DEBUG=True) @app.route('/asyn/', methods=['GET']) def test_asyn_one(): print("asyn has a request!") time.sleep(10) return 'hello asyn' @app.route('/test/', methods=['GET']) def test(): return 'hello test' if __name__ == "__main__": # app.run() http_server = WSGIServer(('', 5000), app) http_server.serve_forever()
關(guān)于monkey.patch_all()
為什么要加monkey.patch_all()這一條語句呢?在gevnet的官網(wǎng)有詳細(xì)的解釋,這里簡單說明一下:
monkey carefully replace functions and classes in the standard socket module with their cooperative counterparts. That way even the modules that are unaware of gevent can benefit from running in a multi-greenlet environment.
翻譯:猴子補(bǔ)丁仔細(xì)的用并行代碼副本替換標(biāo)準(zhǔn)socket模塊的函數(shù)和類,這種方式可以使模塊在不知情的情況下讓gevent更好的運(yùn)行于multi-greenlet環(huán)境中。
測試
打開瀏覽器,首先請求http://127.0.0.1:5000/asyn/,然后
再請求http://127.0.0.1:5000/test/這個(gè)接口十次。如果是一般的Flask框架,后面的接口是沒有響應(yīng)的。
打印內(nèi)容如下:
asyn has a request!
127.0.0.1 - - [2016-10-24 20:45:10] "GET /test/ HTTP/1.1" 200 126 0.000000
127.0.0.1 - - [2016-10-24 20:45:11] "GET /test/ HTTP/1.1" 200 126 0.000000
127.0.0.1 - - [2016-10-24 20:45:11] "GET /test/ HTTP/1.1" 200 126 0.000000
127.0.0.1 - - [2016-10-24 20:45:12] "GET /test/ HTTP/1.1" 200 126 0.000000
127.0.0.1 - - [2016-10-24 20:45:12] "GET /test/ HTTP/1.1" 200 126 0.000998
127.0.0.1 - - [2016-10-24 20:45:13] "GET /test/ HTTP/1.1" 200 126 0.001001
127.0.0.1 - - [2016-10-24 20:45:14] "GET /test/ HTTP/1.1" 200 126 0.000000
127.0.0.1 - - [2016-10-24 20:45:14] "GET /test/ HTTP/1.1" 200 126 0.001014
127.0.0.1 - - [2016-10-24 20:45:15] "GET /test/ HTTP/1.1" 200 126 0.001000
127.0.0.1 - - [2016-10-24 20:45:15] "GET /test/ HTTP/1.1" 200 126 0.000000
127.0.0.1 - - [2016-10-24 20:45:18] "GET /asyn/ HTTP/1.1" 200 126 10.000392
總結(jié)
以上就是本文關(guān)于Flask實(shí)現(xiàn)異步非阻塞請求功能實(shí)例解析的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
SpringBoot FailureAnalyzer實(shí)例使用教程
FailureAnalyzer是一種在啟動(dòng)時(shí)攔截exception并將其轉(zhuǎn)換為human-readable消息的好方法,包含在故障分析中。SpringBoot為application context相關(guān)的exceptions,JSR-303驗(yàn)證等提供了這樣的分析器,實(shí)際上很容易創(chuàng)建自己的2022-12-12Java實(shí)現(xiàn)在線聊天室(層層遞進(jìn))
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)在線聊天室,層層遞進(jìn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09springboot中的Application.properties常用配置
這篇文章主要介紹了springboot中的Application.properties常用配置,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05Spring實(shí)戰(zhàn)之緩存使用condition操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之緩存使用condition操作,結(jié)合實(shí)例形式分析了Spring緩存使用condition具體配置、屬性、領(lǐng)域模型等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2020-01-01java實(shí)現(xiàn)在pdf模板的指定位置插入圖片
這篇文章主要為大家詳細(xì)介紹了java如何實(shí)現(xiàn)在pdf模板的指定位置插入圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10