Domanda

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?

È stato utile?

Soluzione

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):

$ ab -c 200 -n 200 http://localhost:8888/

Completed 100 requests
Completed 200 requests
Finished 200 requests

Concurrency Level:      200
Time taken for tests:   5.048 seconds
Requests per second:    39.62 [#/sec] (mean)
Time per request:       5048.386 [ms] (mean)

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.

Altri suggerimenti

The browsers are known to queue requests to the same domain.

Try opening different browsers (not different browser windows, actually different applications, e.g. FF and Chrome) for different connections.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top