python - Python中的绿色线程和线程

标签 python multithreading pthreads gil green-threads

作为 Wikipedia states :

Green threads emulate multi-threaded environments without relying on any native OS capabilities, and they are managed in user space instead of kernel space, enabling them to work in environments that do not have native thread support.

Python的线程被实现为pthreads(内核线程), 并且由于全局解释器锁 (GIL),Python 进程一次只能运行一个线程。

[问题] 但是对于 Green-threads(或所谓的 greenlet 或 tasklets),

  1. Does the GIL affect them? Can there be more than one greenlet running at a time?
  2. What are the pitfalls of using greenlets or tasklets?
  3. If I use greenlets, how many of them can a process can handle? (I am wondering because in a single process you can open threads up to ulimit(-s, -v) set in your *ix system.)

我需要一点洞察力,如果有人可以分享他们的经验,或者引导我走上正确的道路,这将有所帮助。

最佳答案

您可以将 greenlet 视为更像是协作线程。这意味着没有调度程序在任何给定时刻在您的线程之间先发制人地切换 - 相反,您的 greenlet 自愿/明确地在您的代码中的指定点相互放弃控制。

Does the GIL affect them? Can there be more than one greenlet running at a time?

一次只运行一个代码路径 - 优点是您可以最终控制哪个代码路径。

What are the pitfalls of using greenlets or tasklets?

您需要更加小心 - 写得不好的 greenlet 不会让其他 greenlet 控制。另一方面,由于您知道 greenlet 何时会进行上下文切换,因此您也许可以避免为共享数据结构创建锁。

If I use greenlets, how many of them can a process can handle? (I am wondering because in a single process you can open threads up to umask limit set in your *ix system.)

使用常规线程,您拥有的调度程序开销越多。此外,常规线程仍然具有相对较高的上下文切换开销。 Greenlets 没有与它们相关的这种开销。来自 bottle documentation :

Most servers limit the size of their worker pools to a relatively low number of concurrent threads, due to the high overhead involved in switching between and creating new threads. While threads are cheap compared to processes (forks), they are still expensive to create for each new connection.

The gevent module adds greenlets to the mix. Greenlets behave similar to traditional threads, but are very cheap to create. A gevent-based server can spawn thousands of greenlets (one for each connection) with almost no overhead. Blocking individual greenlets has no impact on the servers ability to accept new requests. The number of concurrent connections is virtually unlimited.

如果您有兴趣,这里还有一些进一步的阅读: http://sdiehl.github.io/gevent-tutorial/

关于python - Python中的绿色线程和线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12758952/

相关文章:

python - Matplotlib:图例中的颜色编码文本而不是线条

在线程中启动的 Python 的 SimpleHTTPServer 不会关闭端口

c - 为什么第二线程中的暂停(2)不返回?

c - 如何将参数从线程传递到c中的函数

创建具有多个参数的函数的pthread

java - 在 Python 中创建 Java DataInputStream 数据

python - 使用两个加法运算符将两个整数相加在 python 中有效吗?

c - 更有效的并发解决方案

Python 2.6 : os. rename() 或 os.renames() 报告 OSError 但文件名为 None

Android 使用 AlarmManager 用闹钟替换 Thread.sleep