5
votes

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
Maybe increase processes = 1? - Hugh Bothwell
if I wanted to serve 2-10 requests then yes, just run multiple processes or threads and call it a day. I don't even need gevent for that. But I'm going to serve thousands simultaneous slow requests, and I obviously need an event-loop for that and I definitely do something wrong... - Arseniy
I think gunicorn's gevent (or eventlet) worker will do what you want, though I haven't used it myself in this way. - dcrosta
Does the example shown here work for you? - Miguel
Try sending requests with curl or anything else than browser as browser has a limit on the number of simultaneous connections to the same site. Or use two different browsers. - jwalker

1 Answers

4
votes

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.