Flask#server.py:
from flask import Flask
import time
app = Flask(__name__)
@app.route("/")
def index():
time.sleep(10)
return "Hello World"
if __name__=='__main__':
app.run()
gunicorn配置#gun.py:
import os
bind='127.0.0.1:5000'
workers=4
backlog=2048
worker_class="gevent" #sync, gevent,meinheld
debug=True
proc_name='/tmp/gunicorn.pid'
pidfile='/tmp/gunicorndebug.log'
loglevel='debug'
测试脚本#test.py:
import time
import requests
s=time.time()
print requests.get("http://127.0.0.1:5000").text
print time.time()-s
然后启动服务:
gunicorn -c gun.py server:app
同时我开启新的两个shell进行测试,几乎同时执行python test.py
测试结果为:
一个为:
Hello World
19.0649909973
另一个为:
Hello World
19.0649909973
本来Flask就是阻塞的,以为加上gunicorn就ok了,但是显然Flask被阻塞了,请问该如何解决呢?之前用tornado加上@asynchronous
就可以解决阻塞问题...
PHP中文网2017-04-17 13:56:07
Do not use time.sleep
but use gevent.sleep
. The mechanisms of the two are different. Tornado uses its own IOLoop, while gevent uses libev (the old version uses libevent). Therefore, only gevent's own sleep can notify libev that the current coroutine is blocked. The monkey patch of gevent also changes all relevant parts of each library to use libev to make gevent work.