Non-blocking concurrent wsgi server

2019-01-19 05:22发布

问题:

I am trying to be able to respond incoming web requests simultaneously, while processing of a request includes quite long IO call. I'm going to use gevent, as it's supposed to be "non-blocking"

The problem I found is that requests are processed sequentially even though I have a lot of gevent threads. For some reason requests get served by single green thread.

I have nginx (with default config which isn't relevant here I think), also I have uwsgi and simple wsgi app that emulates IO-blocking call as gevent.sleep(). Here they are:

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)

then I simultaneously (almost) open two tabs in my browser, and here is what I get as result:

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

As you can see, first call blocked execution of the view. What did I wrong?

回答1:

Send requests with curl or anything else than browser as browser has a limit on the number of simultaneous connections per site or per address. Or use two different browsers.