解释性语言如何避免使用全局解释器锁(GIL)?
CPython uses a Global Interpreter Lock. Linux has removed all traces of the Big Kernel Lock. What is the alternative to these locks? How can a system make full use of a truly multi-core or multi-processor system without grinding everything to a halt?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果 python 使用更高级的垃圾收集器,例如 IBM 的垃圾收集器,则不需要 GIL回收器设置了一种原始的引用计数方法。这是 Unladen Swallow 为提高性能而采取的措施Python。一个更有希望的答案是Stackless Python,它使用自己的微线程实现,而不是依赖于操作像传统 CPython 一样的系统。
A GIL wouldn't be necessary if python used a more advanced Garbage Collector like IBM's Recycler instated of a primitive reference counting method. This is something that Unladen Swallow is doing to improve the performance of python. A more prommising answer is Stackless Python, which uses its own micro-thread implementation instead of relying on the operating system like traditional CPython.
GIL 是特定于进程的,因此您可以通过启动多个 Python 进程来绕过它。 多处理模块 为此提供了一个易于使用的 API。
另一种方法是使用 C 扩展(或编写您自己的扩展),它在执行您需要的数据处理时释放 GIL。
The GIL is process specific, so you can get around it by launching several Python processes. The multiprocessing module provides an easy-to-use API for this.
Another way is to use C-extensions (or write your own) which release the GIL while doing the kind of data processing you need.
简单的。没有可变状态,就像 Haskell 和其他函数式编程语言一样。由于内存中没有任何内容需要更改,因此不需要全局锁。
Simple. Have no mutable state, much like Haskell and other functional programming languages do. Since nothing in memory needs to be changed, no global lock is ever needed.
您可以像 Linux 人员摆脱大内核锁一样摆脱 GIL:只需添加许多更细粒度的锁或使用不需要锁的原子原语。
Python 不这样做的缺点和主要原因是性能。例如,Tcl 解释器没有 GIL,但可以编译为线程和非线程,如果使用线程版本,性能会比单线程情况下降低约 10-20%。所以除非你使用线程,否则它实际上会更慢。已经有Python补丁可以添加更小的锁,但这些补丁对性能的影响更糟,因此被拒绝了。
这只是一种权衡,Python 开发人员认为单线程性能(以及与 C 扩展的向后兼容性)比在 Python 级别使用多个线程的选项重要得多。您仍然可以在 C 扩展中自由使用线程,只是在 python 语言级别上没有意义。
You can get rid of the GIL in quite the same way the Linux guys got rid of the Big Kernel Lock: simply add lots of more fine grained locks or use atomic primitives that don't need locks.
The downside and main reason Python doesn't do it, is performance. For example the Tcl interpreter has no GIL but can be compiled threaded and non-threaded, if you use the threaded version, you get around 10-20% less performance than in the single threaded case. So unless you use threads, it is actually slower. There have been Python patches to add smaller locks, but those had even worse performance impacts, so were rejected.
It is just a trade-off, the python devs decided that single thread performance (and backward compatibility with C-extensions) is much more important than the option to use many threads on the python level. You can still use threads freely inside a C-extension, just not meaningfully on the python language level.