from flask import Flask
import time
app = Flask(__name__)
@app.route('/test)
def sayHello():
time.sleep(10)
return 'hello'
@app.route('/hi')
def sayHi():
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 这个不受其他的影响,能够同时加载?
可以用gevet啊
测试结果:
127.0.0.1 - - [2017-12-12 22:35:10] "GET /test/ HTTP/1.1" 200 126 0.000000
127.0.0.1 - - [2017-12-12 22:35:11] "GET /test/ HTTP/1.1" 200 126 0.000000
127.0.0.1 - - [2017-12-12 22:35:11] "GET /test/ HTTP/1.1" 200 126 0.000000
127.0.0.1 - - [2017-12-12 22:35:12] "GET /test/ HTTP/1.1" 200 126 0.000000
127.0.0.1 - - [2017-12-12 22:35:12] "GET /test/ HTTP/1.1" 200 126 0.000998
127.0.0.1 - - [2017-12-12 22:35:13] "GET /test/ HTTP/1.1" 200 126 0.001001
127.0.0.1 - - [2017-12-12 22:35:14] "GET /test/ HTTP/1.1" 200 126 0.000000
127.0.0.1 - - [2017-12-12 22:35:14] "GET /test/ HTTP/1.1" 200 126 0.001014
127.0.0.1 - - [2017-12-12 22:35:15] "GET /test/ HTTP/1.1" 200 126 0.001000
127.0.0.1 - - [2017-12-12 22:35:15] "GET /test/ HTTP/1.1" 200 126 0.000000
127.0.0.1 - - [2017-12-12 22:35:18] "GET /asyn/ HTTP/1.1" 200 126 10.000392