I’ve just started working with coroutines and have read up on gevent and greenlets. For a test I served this code through gevents pywsgi module:
from gevent.pywsgi import WSGIServer
import gevent
def hello_world(env, start_response):
gevent.sleep(5)
start_response('200 OK', [('Content-Type', 'text/html')])
return ["<b>hello world</b>"]
print 'Serving on 8088...'
WSGIServer(('127.0.0.1', 8888), hello_world).serve_forever()
I expected a result where every request would get a 5 second delay before the text is displayed. However, what happens is that every request gets queued up with the call to gevent.sleep() which makes a second request take almost 10 seconds if it was initiated immediately after the first one.
Isn’t the serve_forever function spawning new greenlets for every request?
What are you using to make the requests? I suspect the problem lies there.
I tested your code with ab (Apache Benchmark) and got this (output edited):
The ab command makes 200 concurrent requests to the gevent server. After five seconds, all requests have completed. If the requests were queued, as you suggest, this benchmark would take 1000 seconds.
I suppose it’s possible that your system doesn’t support greenlets properly, but it seems more likely that the method you are using to test is blocking on each request. I.e. the server is supporting concurrency but your client isn’t.