天天看点

python同时同步发送多个请求_python flask如何解决同时请求同一个请求的阻塞问题?...

可以用gevet啊

from gevent import monkey

from gevent.pywsgi import WSGIServer

monkey.patch_all()

from flask import Flask

import time

app = Flask(__name__)

@app.route('/test',methods=['GET'])

def sayHello():

time.sleep(10)

return 'hello'

@app.route('/hi',methods=['GET'])

def sayHi():

return 'hi'

if __name__ =='__main__':

http_server = WSGIServer(('', 5000), app)

http_server.serve_forever()

测试结果:

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