简体   繁体   English

为什么使用 gevent.joinall() 而不是 pool.imap_unordered() 来运行 Greenlets?

[英]Why Use gevent.joinall() Instead of pool.imap_unordered() to Run Greenlets?

The title says it all.标题说明了一切。 It seems better and faster to use one of the methods belonging to gevent.Pool to run greenlets in parallel (sort-of) in a pool, as opposed to gevent.joinall().与 gevent.joinall() 相比,使用属于 gevent.Pool 的方法之一在池中并行(排序)运行 greenlets 似乎更好更快。 What are the pros and cons of each approach?每种方法的优缺点是什么?

I think the key difference is not raw performance but instead performance management.我认为关键的区别不是原始性能,而是性能管理。 When you use gevent.joinall() you have to do your own management of how many greenlets exist at once.当您使用 gevent.joinall() 时,您必须自己管理一次存在多少绿叶。 The naive implementation would create as many as might be needed by the request for the computation.天真的实现将创建计算请求可能需要的数量。

On the other hand gevent.Pool can easily be configured to cap how many are running at once and thus protect against running your application out of resources.另一方面,gevent.Pool 可以很容易地配置为限制同时运行的数量,从而防止您的应用程序在资源不足的情况下运行。

As usual, it's tradeoffs.像往常一样,这是权衡。 Your pool may run slower because it potentially won't allow as many greenlets to run as would a naive implementation using gevent.joinall(), however, you are less likely to run your application out of resources (and cascade into other errors).您的池可能运行速度较慢,因为它可能不会像使用 gevent.joinall() 的幼稚实现那样允许运行尽可能多的 greenlet,但是,您不太可能在资源不足的情况下运行应用程序(并级联到其他错误)。

Ultimately you have to answer questions like this: Are you likely to get too large of requests?最终,您必须回答这样的问题:您是否可能收到过多的请求? Do you have plenty of resources to draw from?你有足够的资源可以利用吗? Is raw peak performance more important than average reliability?原始峰值性能比平均可靠性更重要吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM