Python 3.1 中的 GIL

发布于 2024-07-29 18:49:39 字数 40 浏览 6 评论 0 原文

有谁知道Python 3.1中全局解释器锁对C++多线程集成的命运

Does anybody knows fate of Global Interpreter Lock in Python 3.1 against C++ multithreading integration

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

迷离° 2024-08-05 18:49:39

GIL 在 CPython 3.1 中仍然存在; Unladen Swallow 项目的目标(以及许多其他性能提升)最终将其删除,但是它距其目标还有一段距离,并且正在首先开发 2.6,目的是最终将 2.y 版本完成时当前的 x 移植到 3.x。 目前,多处理(而不是线程)仍然是在 CPython 中使用多核的选择方式(IronPython 和 Jython 也很好,但它们目前不支持 Python 3,也不能让 C++ 集成变得那么容易;- )。

GIL is still there in CPython 3.1; the Unladen Swallow projects 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;-).

梦一生花开无言 2024-08-05 18:49:39

Python 3.2 的 GIL 将发生重大变化。 查看Python 3.2 的新增功能 ,以及在邮件列表中启动它的线程

虽然这些变化并不意味着 GIL 的终结,但它们预示着潜在的巨大性能提升。

更新

  • Antoine Pitrou 在 3.2 中使用新的 GIL 带来的总体性能提升可以忽略不计,而是专注于改善争用问题 在某些极端情况下出现。
  • David Beazley 做出了令人钦佩的努力,实现了调度程序,以在 CPU 和 IO 绑定线程时显着提高性能混杂,不幸被击落。
  • Unladen Swallow 工作建议合并到 Python 3.3 中,但这已经由于该项目缺乏成果而被撤回。 PyPy 现在是首选项目,目前是 请求资金以添加 Python3k 支持。 目前 PyPy 成为默认的可能性很小。

过去 15 年里,人们一直在努力从 CPython 中删除 GIL,但在可预见的未来,它仍将继续存在。

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.

While the changes don't signify the end of the GIL, they herald potentially enormous performance gains.

Update

  • The general performance gains with the new GIL in 3.2 by Antoine Pitrou were negligible, and instead focused on improving contention issues that arise in certain corner cases.
  • An admirable effort by 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 merging in Python 3.3, but this has been withdrawn due to lack of results in that project. PyPy is now the preferred project and is currently requesting funding to add Python3k support. There's very little chance that PyPy will become the default at present.

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.

你是我的挚爱i 2024-08-05 18:49:39

GIL 不会影响不使用 python 对象的代码。 在Numpy中,我们发布了用于计算代码(线性代数调用等...)的GIL,底层代码可以自由使用多线程(实际上,那些通常是对python一无所知的第三方库)

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)

放飞的风筝 2024-08-05 18:49:39

GIL 是个好东西。

只需让您的 C++ 应用程序在执行多线程工作时释放 GIL 即可。 Python 代码将继续在其他线程中运行,不会被破坏。 仅当必须接触 python 对象时才获取 GIL。

The GIL is a good thing.

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.

微凉徒眸意 2024-08-05 18:49:39

我想总会有一个GIL。
原因是性能。 使所有低级访问线程安全 - 意味着在每个哈希操作等周围放置互斥体是很繁重的。 请记住,像“Might”这样的简单语句

self.foo(self.bar, 3, val)

目前可能已经有至少 3 个(如果 val 是全局的)哈希表查找,如果方法缓存不热(取决于类的继承深度),甚至可能更多,

这是昂贵的 - 那是为什么 Java 放弃了这个想法并引入了不使用监视器调用的哈希表,以摆脱其“Java 很慢”的商标。

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

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)

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.

妄司 2024-08-05 18:49:39

据我了解,“brainfuck”调度程序将取代 python 3.2 中的 GIL

BFS bainfuck 调度程序

As I understand it the "brainfuck" scheduler will replace the GIL from python 3.2

BFS bainfuck scheduler

书间行客 2024-08-05 18:49:39

如果 GIL 妨碍,只需使用 multiprocessing 模块。 它生成新进程,但使用线程模型和(大部分)API。 换句话说,您可以以类似线程的方式实现基于进程的并行性。

If the GIL is getting in the way, just use the multiprocessing module. 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.

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