Python 3.1 中的 GIL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1222929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GIL in Python 3.1
提问by Dewfy
Does anybody knows fate of Global Interpreter Lock in Python 3.1 against C++ multithreading integration
有没有人知道 Python 3.1 中全局解释器锁对 C++ 多线程集成的命运
回答by Alex Martelli
GIL is still there in CPython 3.1; the Unladen Swallowprojects aims (among many other performance boosts) to eventually remove it, but it's still a way from its goals, and is working on 2.6 first with the intent of eventually porting to 3.x for whatever x will be current by the time the 2.y version is considered to be done. For now, multiprocessing (instead of threading) remains the way of choice for using multiple cores in CPython (IronPython and Jython are fine too, but they don't support Python 3 currently, nor do they make C++ integration all that easy either;-).
GIL 在 CPython 3.1 中仍然存在;在空载燕子项目目标(其中包括许多其他的性能提升),最终将其删除,但它仍然从它的目标的方式,并与意图的最终移植到3.x的工作2.6首先不管什么x是当前2.y 版本被认为完成的时间。目前,多处理(而不是线程)仍然是在 CPython 中使用多核的首选方式(IronPython 和 Jython 也很好,但它们目前不支持 Python 3,也没有使 C++ 集成变得那么容易;- )。
回答by Matt Joiner
Significant changes will occur in the GIL for Python 3.2. Take a look at the What's New for Python 3.2, and the thread that initiated it in the mailing list.
Python 3.2 的 GIL 将发生重大变化。查看Python 3.2 的新增功能,以及在邮件列表中启动它的线程。
While the changes don't signify the end of the GIL, they herald potentially enormous performance gains.
虽然这些变化并不意味着 GIL 的结束,但它们预示着潜在的巨大性能提升。
Update0
更新0
- The general performance gains with the new GIL in 3.2 by Antoine Pitrou were negligible, and instead focused on improving contention issuesthat arise in certain corner cases.
- An admirable effortby David Beazley was made to implement a scheduler to significantly improve performance when CPU and IO bound threads are mixed, which was unfortunately shot down.
- The Unladen Swallow work was proposed for mergingin Python 3.3, but this has been withdrawn due to lack of results in that project. PyPyis now the preferred project and is currently requesting fundingto add Python3k support. There's very little chance that PyPy will become the default at present.
- Antoine Pitrou 在 3.2 中使用新 GIL 的总体性能提升可以忽略不计,而是专注于改善某些极端情况下出现的争用问题。
- 一个令人钦佩的努力,由大卫比兹利是为了实现一个调度器显著提高性能时,CPU和IO绑定线程混合,这是不幸击落。
- Unladen Swallow 的工作被提议合并到 Python 3.3 中,但由于该项目缺乏结果,该工作已被撤回。PyPy现在是首选项目,目前正在申请资金以添加 Python3k 支持。目前,PyPy 成为默认值的可能性很小。
Efforts have been made for the last 15 years to remove the GIL from CPython but for the foreseeable future it is here to stay.
过去 15 年来一直在努力从 CPython 中删除 GIL,但在可预见的未来,它会继续存在。
回答by David Cournapeau
The GIL will not affect your code which does not use python objects. In Numpy, we release the GIL for computational code (linear algebra calls, etc...), and the underlying code can use multithreading freely (in fact, those are generally 3rd party libraries which do not know anything about python)
GIL 不会影响不使用 python 对象的代码。在Numpy中,我们为计算代码(线性代数调用等...)发布了GIL,底层代码可以自由使用多线程(其实那些一般都是对python一无所知的3rd方库)
回答by nosklo
The GIL is a good thing.
GIL 是个好东西。
Just make your C++ application release the GIL while it is doing its multithreaded work. Python code will continue to run in the other threads, unspoiled. Only acquire the GIL when you have to touch python objects.
只需让您的 C++ 应用程序在执行多线程工作时释放 GIL。Python 代码将继续在其他线程中运行,未受破坏。仅当您必须接触 python 对象时才获取 GIL。
回答by Lothar
I guess there will always be a GIL. The reason is performance. Making all the low level access thread safe - means putting a mutex around each hash operation etc. is heavy. Remember that a simple statement like
我想总会有一个 GIL。原因是性能。使所有低级访问线程安全 - 意味着在每个散列操作等周围放置一个互斥锁是很重的。请记住,一个简单的语句,如
self.foo(self.bar, 3, val)
Might already have at least 3 (if val is a global) hashtable lookups at the moment and maybe even much more if the method cache is not hot (depending on the inheritance depth of the class)
目前可能已经至少有 3 个(如果 val 是全局的)哈希表查找,如果方法缓存不热,则可能更多(取决于类的继承深度)
It's expensive - that's why Java dropped the idea and introduced hashtables which do not use a monitor call to get rid of its "Java Is Slow" trademark.
它很昂贵——这就是为什么 Java 放弃了这个想法并引入了不使用监视器调用的哈希表来摆脱它的“Java Is Slow”商标。
回答by sayth
As I understand it the "brainfwor" scheduler will replace the GIL from python 3.2
据我了解,“brainfwor”调度程序将替换 python 3.2 中的 GIL
回答by Alex
If the GIL is getting in the way, just use the multiprocessingmodule. It spawns new processes but uses the threading model and (most of the) api. In other words, you can do process-based parallelism in a thread-like way.
如果 GIL 妨碍了,只需使用多处理模块。它产生新进程,但使用线程模型和(大部分)api。换句话说,您可以以类似于线程的方式进行基于进程的并行处理。