Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
583 views
in Technique[技术] by (71.8m points)

python - Non-blocking concurrent wsgi server

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...