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

查看:91
本文介绍了什么是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天全站免登陆