使用Gevent和WSGI阻止呼叫 [英] Blocking calls with Gevent and WSGI

查看:103
本文介绍了使用Gevent和WSGI阻止呼叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用协程,并阅读了gevent和greenlets.为了进行测试,我通过gevents pywsgi模块提供了以下代码:

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

我希望得到一个结果,其中每个请求在显示文本之前都会有5秒的延迟.但是,发生的情况是,每个请求都与对gevent.sleep()的调用排队,如果第二个请求在第一个请求之后立即启动,则将花费将近10秒.

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.

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

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.

我用ab(Apache Benchmark)测试了您的代码,并进行了修改(输出已编辑):

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)

ab命令向gevent服务器发出200个并发请求.五秒钟后,所有请求均已完成.如您所建议,如果将请求排队,则此基准测试将花费1000秒.

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.

我想您的系统可能不正确地支持greenlets,但是您似乎要使用的测试方法似乎对每个请求都进行了阻止. IE.服务器支持并发,但您的客户端不支持.

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.

这篇关于使用Gevent和WSGI阻止呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆