[英]Why Use Gevent Pool to Manage Greenlet Connections in a Server?
I am working with a Python server which spawns a greenlet for each connection to the server.我正在使用 Python 服务器,它为与服务器的每个连接生成一个 greenlet。 Currently, the server doesn't make use of a greenlet pool.
目前,服务器不使用 greenlet 池。 While it was my hunch that using a pool would improve performance (mainly response time and requests-per-second throughput), in my trial-and-error implementing a pool of greenlets, there doesn't seem be much performance benefit over just using Gevent.spawn() for each greenlet/connection.
虽然我的预感是使用池可以提高性能(主要是响应时间和每秒请求的吞吐量),但在我实现 greenlets 池的反复试验中,似乎与仅使用相比没有太大的性能优势每个绿灯/连接的 Gevent.spawn()。
I have seen this question, which is helpful, although I am curious about the application of a greenlet pool, like Gevent Pool, in a server.我已经看到了这个问题,这很有帮助,尽管我对在服务器中应用 greenlet 池(如Gevent 池)感到好奇。 Is this a useful pattern, a la thread pool?
这是一个有用的模式,一个线程池吗? Or, does using a Pool not matter in the case of a server, since Greenlets are so lightweight compared with threads?
或者,在服务器的情况下使用池是否无关紧要,因为 Greenlets 与线程相比是如此轻量级?
Greenlets are lightweight but they do consume memory. Greenlets 是轻量级的,但它们确实消耗 memory。 So, even though the number of greenlets a process can support is going to be much larger than the number of threads the OS can support, there is still a cost to them.
因此,即使一个进程可以支持的 greenlet 数量将远远大于操作系统可以支持的线程数量,它们仍然需要付出代价。 So a pool is still a useful tool for limiting the number of greenlets that can be spawned - but its size would likely be best set considerably larger than a limit for actual threads would be.
因此,池仍然是限制可以生成的 greenlet 数量的有用工具 - 但最好将其大小设置为比实际线程的限制大得多。
Also, due to their cooperative multitasking nature, the latency on each request (assuming each new request is handled by a new greenlet) would continue to rise as the number of greenlets increases.此外,由于它们的协作多任务性质,每个请求的延迟(假设每个新请求都由新的 greenlet 处理)将随着 greenlet 数量的增加而继续上升。 There's a tradeoff between allowing more requests at once and creating poor UX when each request takes an increasing amount of time to complete.
当每个请求需要越来越多的时间才能完成时,需要在一次允许更多请求和创建糟糕的用户体验之间进行权衡。 It's sometimes better to cap your incoming load and reject new requests - and a pool is a useful way to do that.
有时最好限制传入的负载并拒绝新请求 - 池是一种有用的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.