绿线程和Python中的线程 [英] Green-threads and thread in Python

查看:119
本文介绍了绿线程和Python中的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为> 维基百科州 :

绿色线程在不依赖任何本机OS功能的情况下模拟多线程环境,并且它们在用户空间而不是内核空间中进行管理,从而使它们可以在不具有本机线程支持的环境中工作.

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 (kernel threads), 并且由于全局解释器锁(GIL),Python进程一次只能运行一个线程.

Python's threads are implemented as pthreads (kernel threads), and because of the global interpreter lock (GIL), a Python process only runs one thread at a time.

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

[QUESTION] But in the case of Green-threads (or so-called greenlet or tasklets),

  1. GIL对他们有影响吗?可以有一个以上的greenlet吗 一次运行?
  2. 使用greenlet或tasklet有什么陷阱?
  3. 如果我使用greenlet,一个进程可以处理其中的几个? (我想知道,因为您可以在单个过程中打开最多 在您的* ix系统中设置的 ulimit ( -s -v ).
  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.)

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

I need a little insight, and it would help if someone could share their experience, or guide me to the right path.

推荐答案

您可以将greenlets视为协作线程.这意味着在任何给定的时刻都没有调度程序抢先在线程之间进行切换-而是您的greenlet在代码中的指定点主动/明确地放弃了彼此的控制权.

You can think of greenlets more like cooperative threads. What this means is that there is no scheduler pre-emptively switching between your threads at any given moment - instead your greenlets voluntarily/explicitly give up control to one another at specified points in your code.

GIL是否会影响他们?是否可以运行多个greenlet 一次?

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

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

Only one code path is running at a time - the advantage is you have ultimate control over which one that is.

使用greenlet或tasklet有什么陷阱?

What are the pitfalls of using greenlets or tasklets?

您需要更加小心-编写不正确的Greenlet不会使其他Greenlet受到控制.另一方面,由于您知道何时greenlet将进行上下文切换,因此您可以为共享数据结构创建锁.

You need to be more careful - a badly written greenlet will not yield control to other greenlets. On the other hand, since you know when a greenlet will context switch, you may be able to get away with not creating locks for shared data-structures.

如果我使用greenlet,一个进程可以处理其中的几个? (我想知道是因为您可以在单个进程中打开线程,直到在* ix系统中设置的umask限制.)

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没有与此相关的开销.从瓶文档:

With regular threads, the more you have the more scheduler overhead you have. Also regular threads still have a relatively high context-switch overhead. Greenlets do not have this overhead associated with them. From the 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.

gevent模块将greenlets添加到混合中.小绿人行为相似 到传统线程,但是创建起来非常便宜.基于gevent 服务器可以生成数千个greenlet(每个连接一个) 几乎没有开销.阻止单个greenlet不会影响 服务器具有接受新请求的能力.并发数 连接几乎是无限的.

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/

There's also some further reading here if you're interested: http://sdiehl.github.io/gevent-tutorial/

这篇关于绿线程和Python中的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆