解释性语言如何避免使用全局解释器锁(GIL)?

发布于 2024-10-20 02:28:40 字数 263 浏览 3 评论 0原文

CPython 使用全局解释器锁。 Linux 已删除大内核锁的所有痕迹。这些锁的替代品是什么?系统如何才能充分利用真正的多核或多处理器系统,而不会使一切都陷入停顿?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

﹎☆浅夏丿初晴 2024-10-27 02:28:40

如果 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.

简单 2024-10-27 02:28:40

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.

眉目亦如画i 2024-10-27 02:28:40

简单的。没有可变状态,就像 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.

谎言 2024-10-27 02:28:40

您可以像 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文