CPython 中的全局解释器锁 (GIL) 是什么? [英] What is the global interpreter lock (GIL) in CPython?

查看:30
本文介绍了CPython 中的全局解释器锁 (GIL) 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是全局解释器锁,为什么会出现问题?

What is a global interpreter lock and why is it an issue?

围绕从 Python 中删除 GIL 已经产生了很多噪音,我想了解为什么这如此重要.我自己从来没有写过编译器和解释器,所以不要吝啬细节,我可能需要他们来理解.

A lot of noise has been made around removing the GIL from Python, and I'd like to understand why that is so important. I have never written a compiler nor an interpreter myself, so don't be frugal with details, I'll probably need them to understand.

推荐答案

Python 的 GIL 旨在序列化从不同线程对解释器内部的访问.在多核系统上,这意味着多线程不能有效地利用多核.(如果 GIL 没有导致这个问题,大多数人不会关心 GIL——它只是因为多核系统的日益流行而被提出来.)如果你想详细了解它,您可以查看此视频或查看这组幻灯片.可能信息太多,但您确实要求提供详细信息:-)

Python's GIL is intended to serialize access to interpreter internals from different threads. On multi-core systems, it means that multiple threads can't effectively make use of multiple cores. (If the GIL didn't lead to this problem, most people wouldn't care about the GIL - it's only being raised as an issue because of the increasing prevalence of multi-core systems.) If you want to understand it in detail, you can view this video or look at this set of slides. It might be too much information, but then you did ask for details :-)

请注意,Python 的 GIL 只是 CPython(参考实现)的真正问题.Jython 和 IronPython 没有 GIL.作为 Python 开发人员,除非您正在编写 C 扩展,否则您通常不会遇到 GIL.C 扩展编写者需要在他们的扩展阻塞 I/O 时释放 GIL,以便 Python 进程中的其他线程有机会运行.

Note that Python's GIL is only really an issue for CPython, the reference implementation. Jython and IronPython don't have a GIL. As a Python developer, you don't generally come across the GIL unless you're writing a C extension. C extension writers need to release the GIL when their extensions do blocking I/O, so that other threads in the Python process get a chance to run.

这篇关于CPython 中的全局解释器锁 (GIL) 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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