fromflaskimportFlaskimporttimeapp=Flask(__name__)@app.route('/test)defsayHello():time.sleep(10)return'hello'@app.route('/hi')defsayHi():return'hi'if__name__=='__main__'app.run(debug=True,threaded=True)如上述代码所示,倘若在浏览器打开多张空白页,然后都去请求127.0.0.1:5000/test,会发现进入了阻塞状态,每一张页面会等待它之前的那个请求结束后在加载当前页面。但是去访问127.0.0.1:5000/hi却不会收到前面那个网址的影响所以,我应该如何实现我多个请求去访问127.0.0.1:5000/test这个不受其他的影响,能够同时加载?
2 回答
米脂
TA贡献1836条经验 获得超3个赞
可以用gevet啊fromgeventimportmonkeyfromgevent.pywsgiimportWSGIServermonkey.patch_all()fromflaskimportFlaskimporttimeapp=Flask(__name__)@app.route('/test',methods=['GET'])defsayHello():time.sleep(10)return'hello'@app.route('/hi',methods=['GET'])defsayHi():return'hi'if__name__=='__main__':http_server=WSGIServer(('',5000),app)http_server.serve_forever()测试结果:127.0.0.1--[2017-12-1222:35:10]"GET/test/HTTP/1.1"2001260.000000127.0.0.1--[2017-12-1222:35:11]"GET/test/HTTP/1.1"2001260.000000127.0.0.1--[2017-12-1222:35:11]"GET/test/HTTP/1.1"2001260.000000127.0.0.1--[2017-12-1222:35:12]"GET/test/HTTP/1.1"2001260.000000127.0.0.1--[2017-12-1222:35:12]"GET/test/HTTP/1.1"2001260.000998127.0.0.1--[2017-12-1222:35:13]"GET/test/HTTP/1.1"2001260.001001127.0.0.1--[2017-12-1222:35:14]"GET/test/HTTP/1.1"2001260.000000127.0.0.1--[2017-12-1222:35:14]"GET/test/HTTP/1.1"2001260.001014127.0.0.1--[2017-12-1222:35:15]"GET/test/HTTP/1.1"2001260.001000127.0.0.1--[2017-12-1222:35:15]"GET/test/HTTP/1.1"2001260.000000127.0.0.1--[2017-12-1222:35:18]"GET/asyn/HTTP/1.1"20012610.000392
暮色呼如
TA贡献1853条经验 获得超9个赞
我认为访问hi也会受到test的影响。言归正传,你可以使用gevent,在文件开头:fromgeventimportmonkeymonkey.patch_all()
添加回答
举报
0/150
提交
取消