【问题标题】:Blocking calls with Gevent and WSGI使用 Gevent 和 WSGI 阻止调用
【发布时间】:2011-10-29 05:20:56
【问题描述】:

我刚刚开始使用协程并阅读了 gevent 和 greenlets。对于测试,我通过 gevents pywsgi 模块提供了此代码:

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

我预计每个请求都会在显示文本之前延迟 5 秒的结果。但是,发生的情况是,每个请求都会在调用 gevent.sleep() 时排队,如果在第一个请求之后立即发起第二个请求,则它会花费将近 10 秒。

serve_forever 函数不是为每个请求生成新的 greenlets 吗?

【问题讨论】:

    标签: wsgi blocking coroutine gevent


    【解决方案1】:

    您使用什么来提出请求?我怀疑问题出在那儿。

    我使用 ab(Apache Benchmark)测试了您的代码并得到了这个(输出已编辑):

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

    ab 命令向 gevent 服务器发出 200 个并发请求。五秒钟后,所有请求都已完成。如果按照您的建议将请求排队,则此基准测试需要 1000 秒。

    我想您的系统可能无法正确支持 greenlets,但您用于测试的方法似乎更有可能在每个请求上都被阻止。 IE。服务器支持并发,但您的客户端不支持。

    【讨论】:

      【解决方案2】:

      众所周知,浏览器会将请求排队到同一域。

      尝试为不同的连接打开不同的浏览器(不是不同的浏览器窗口,实际上是不同的应用程序,例如 FF 和 Chrome)。

      【讨论】:

        猜你喜欢
        • 2016-07-14
        • 1970-01-01
        • 1970-01-01
        • 2013-08-19
        • 2016-06-01
        • 2010-11-16
        • 2011-10-13
        • 1970-01-01
        • 2014-09-13
        相关资源
        最近更新 更多