本文介绍了格林莱特线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是gevents和greenlets的新手。我发现了一些关于如何使用greenlets的好的文档,但没有一个给我说明如何和何时使用greenlets的理由。

I am new to gevents and greenlets. I found some good documentation on how to work with them, but none gave me justification on how and when I should use greenlets!


  • 什么是


  • What are they really good at?
  • Is it a good idea to use them in a proxy server or not?
  • Why not threads?

我不知道如果他们基本上是协同例程,他们如何能提供并发性。

What I am not sure about is how they can provide us with concurrency if they're basically co-routines.

推荐答案

Greenlets提供并发,但不是 并行性。并发是指代码可以独立于其他代码运行。并行性是同时执行并发代码。当在用户空间中有很多工作要做时,并行性是特别有用的,这通常是CPU负载较重的东西。

Greenlets provide concurrency but not parallelism. Concurrency is when code can run independently of other code. Parallelism is the execution of concurrent code simultaneously. Parallelism is particularly useful when there's a lot of work to be done in userspace, and that's typically CPU-heavy stuff. Concurrency is useful for breaking apart problems, enabling different parts to be scheduled and managed more easily in parallel.

Greenlets真正在网络编程中发挥作用,其中与一个套接字的交互可以独立发生的与其他套接字的交互。这是并发的典型示例。因为每个greenlet都在其自己的上下文中运行,所以您可以继续使用同步API而无需线程。这是很好的,因为线程在虚拟内存和内核开销方面非常昂贵,因此您可以用线程实现的并发性显着降低。此外,由于GIL,Python中的线程更加昂贵,并且比通常更有限。并发的替代方法通常是像Twisted,libevent,libuv,node.js等项目,其中所有代码共享相同的执行上下文,并注册事件处理程序。

Greenlets really shine in network programming where interactions with one socket can occur independently of interactions with other sockets. This is a classic example of concurrency. Because each greenlet runs in its own context, you can continue to use synchronous APIs without threading. This is good because threads are very expensive in terms of virtual memory and kernel overhead, so the concurrency you can achieve with threads is significantly less. Additionally, threading in Python is more expensive and more limited than usual due to the GIL. Alternatives to concurrency are usually projects like Twisted, libevent, libuv, node.js etc, where all your code shares the same execution context, and register event handlers.

使用greenlets(具有适当的网络支持,例如通过gevent)用于编写代理的好主意,因为您的请求处理能够独立执行,并且应该这样写。

It's an excellent idea to use greenlets (with appropriate networking support such as through gevent) for writing a proxy, as your handling of requests are able to execute independently and should be written as such.

Greenlets提供并发性是因为我之前提供的原因。并发不是并行性。通过隐藏事件注册并为通常阻止当前线程的调用执行调度,像gevent这样的项目公开了这种并发性,而不需要更改异步API,并且系统成本显着降低。

Greenlets provide concurrency for the reasons I gave earlier. Concurrency is not parallelism. By concealing event registration and performing scheduling for you on calls that would normally block the current thread, projects like gevent expose this concurrency without requiring change to an asynchronous API, and at significantly less cost to your system.





  • Concurrency is not parallelism
  • Threads vs. processes
  • Multiprocessing vs. threads
  • GIL vs. CPython

这篇关于格林莱特线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 17:06