使用Gevent和WSGI阻止调用(Blocking calls with Gevent and WSGI)

编程入门 行业动态 更新时间:2024-10-25 18:34:29
使用Gevent和WSGI阻止调用(Blocking calls with Gevent and WSGI)

我刚刚开始使用协同程序,并阅读了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吗?

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?

最满意答案

你用什么来提出要求? 我怀疑那里存在问题。

我用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秒。

我想你的系统可能没有正确支持greenlet,但是你用来测试的方法似乎更有可能阻止每个请求。 即服务器支持并发,但客户端不支持。

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.

更多推荐

本文发布于:2023-07-29 20:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319590.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:WSGI   Gevent   calls   Blocking

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!