gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:45:47 29 4
gpt4 key购买 nike

作为 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com