Flask实现异步非阻塞请求功能(flask 异步请求)

网友投稿 464 2022-09-04


Flask实现异步非阻塞请求功能(flask 异步请求)

pip install gevent

关于gevent

Gevent 是一个 Python 并发网络库,它使用了基于 libevent 事件循环的 greenlet 来提供一个高级同步 API。下面是代码示例:

from gevent.wsgi import WSGIServerfrom yourapplication import app= WSGIServer(('', 5000), app)coding=utf-8# Python Version: 3.5.1# Flaskfrom flask import Flask, request, g# geventfrom gevent import monkeyfrom gevent.pywsgi import WSGIServermonkey.patch_all()# gevent endimport timeapp = 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() = WSGIServer(('', 5000), app) has a request!127.0.0.1 - - [2016-10-24 20:45:10] "GET /test/ HTTP/1.1" 200 126 0.000000127.0.0.1 - - [2016-10-24 20:45:11] "GET /test/ HTTP/1.1" 200 126 0.000000127.0.0.1 - - [2016-10-24 20:45:11] "GET /test/ HTTP/1.1" 200 126 0.000000127.0.0.1 - - [2016-10-24 20:45:12] "GET /test/ HTTP/1.1" 200 126 0.000000127.0.0.1 - - [2016-10-24 20:45:12] "GET /test/ HTTP/1.1" 200 126 0.000998127.0.0.1 - - [2016-10-24 20:45:13] "GET /test/ HTTP/1.1" 200 126 0.001001127.0.0.1 - - [2016-10-24 20:45:14] "GET /test/ HTTP/1.1" 200 126 0.000000127.0.0.1 - - [2016-10-24 20:45:14] "GET /test/ HTTP/1.1" 200 126 0.001014127.0.0.1 - - [2016-10-24 20:45:15] "GET /test/ HTTP/1.1" 200 126 0.001000127.0.0.1 - - [2016-10-24 20:45:15] "GET /test/ HTTP/1.1" 200 126 0.000000127.0.0.1 - - [2016-10-24 20:45:18] "GET /asyn/ HTTP/1.1" 200 126 10.000392


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:flask-session(flasksession更新)
下一篇:Mybatis plus where添加括号方式
相关文章

 发表评论

暂时没有评论,来抢沙发吧~