非阻塞并发wsgi服务器
我正在尝试能够同时响应传入的Web请求,而处理请求包括很长的IO调用。我将使用gevent,因为它应该是“非阻塞的”
我发现的问题是,即使我有很多gevent线程,请求也会按顺序处理。由于某些原因,请求由单个绿色线程处理。
我有nginx(默认配置与我在这里不相关),也有uwsgi和简单的wsgi应用程序,将IO阻塞调用模拟为gevent.sleep()。他们来了:
uwsgi.ini
[uwsgi]
chdir = /srv/website
home = /srv/website/env
module = wsgi:app
socket = /tmp/uwsgi_mead.sock
#daemonize = /data/work/zx900/mob-effect.mead/logs/uwsgi.log
processes = 1
gevent = 100
gevent-monkey-patch
wsgi.py
import gevent
import time
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
t0 = time.time()
gevent.sleep(10.0)
t1 = time.time()
return "{1} - {0} = {2}".format(t0, t1, t1 - t0)
然后我同时(几乎)在浏览器中打开了两个标签,这是我得到的结果:
1392297388.98 - 1392297378.98 = 10.0021491051
# first tab, processing finished at 1392297378.98
1392297398.99 - 1392297388.99 = 10.0081849098
# second tab, processing started at 1392297398.99
如您所见,第一次调用阻止了视图的执行。我怎么了