這篇文章主要介紹Flask如何實(shí)現(xiàn)異步非阻塞請求功能,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供景洪網(wǎng)站建設(shè)、景洪做網(wǎng)站、景洪網(wǎng)站設(shè)計(jì)、景洪網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、景洪企業(yè)網(wǎng)站模板建站服務(wù),十載景洪做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
具體實(shí)現(xiàn)如下。
最近做物聯(lián)網(wǎng)項(xiàng)目的時(shí)候需要搭建一個(gè)異步非阻塞的HTTP服務(wù)器,經(jīng)過查找資料,發(fā)現(xiàn)可以使用gevent包。
Gevent 是一個(gè) Python 并發(fā)網(wǎng)絡(luò)庫,它使用了基于 libevent 事件循環(huán)的 greenlet 來提供一個(gè)高級(jí)同步 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()
為什么要加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
以上是“Flask如何實(shí)現(xiàn)異步非阻塞請求功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前題目:Flask如何實(shí)現(xiàn)異步非阻塞請求功能
分享鏈接:http://jinyejixie.com/article42/ijgghc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、定制網(wǎng)站、軟件開發(fā)、自適應(yīng)網(wǎng)站、做網(wǎng)站、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)