Python高级特性:全局解释器锁GIL基本概念
来源:互联网 发布:眼镜 知乎 编辑:程序博客网 时间:2024/06/03 16:14
简介
本文主要介绍全局解释器锁(Global Interceptor Lock)GIL的基础知识,涉及基本概念,工作原理,对多线程性能影响等。
什么是全局解释器锁GIL
Python代码的执行由Python 虚拟机(也叫解释器主循环,CPython版本)来控制,Python 在设计之初就考虑到要在解释器的主循环中,同时只有一个线程在执行,即在任意时刻,只有一个线程在解释器中运行。对Python 虚拟机的访问由全局解释器锁(GIL)来控制,正是这个锁能保证同一时刻只有一个线程在运行。
在多线程环境中,Python 虚拟机按以下方式执行:
1. 设置GIL
2. 切换到一个线程去运行
3. 运行:
a. 指定数量的字节码指令,或者
2. 切换到一个线程去运行
3. 运行:
a. 指定数量的字节码指令,或者
b. 线程主动让出控制(可以调用time.sleep(0))
4. 把线程设置为睡眠状态
5. 解锁GIL
6. 再次重复以上所有步骤
4. 把线程设置为睡眠状态
5. 解锁GIL
6. 再次重复以上所有步骤
在调用外部代码(如C/C++扩展函数)的时候,GIL 将会被锁定,直到这个函数结束为止(由于在这期间没有Python 的字节码被运行,所以不会做线程切换)。
全局解释器锁GIL设计理念与限制
GIL的设计简化了CPython的实现,使得对象模型,包括关键的内建类型如字典,都是隐含可以并发访问的。锁住全局解释器使得比较容易的实现对多线程的支持,但也损失了多处理器主机的并行计算能力。
但是,不论标准的,还是第三方的扩展模块,都被设计成在进行密集计算任务是,释放GIL。
还有,就是在做I/O操作时,GIL总是会被释放。对所有面向I/O 的(会调用内建的操作系统C 代码的)程序来说,GIL 会在这个I/O 调用之前被释放,以允许其它的线程在这个线程等待I/O 的时候运行。如果是纯计算的程序,没有 I/O 操作,解释器会每隔 100 次操作就释放这把锁,让别的线程有机会执行(这个次数可以通过 sys.setcheckinterval 来调整)如果某线程并未使用很多I/O 操作,它会在自己的时间片内一直占用处理器(和GIL)。也就是说,I/O 密集型的Python 程序比计算密集型的程序更能充分利用多线程环境的好处。
下面是Python 2.7.9手册中对GIL的简单介绍:
The mechanism used by the CPython interpreter to assure that only one thread executes Python bytecode at a time. This simplifies the CPython implementation by making the object model (including critical built-in types such as dict) implicitly safe against concurrent access. Locking the entire interpreter makes it easier for the interpreter to be multi-threaded, at the expense of much of the parallelism afforded by multi-processor machines.
However, some extension modules, either standard or third-party, are designed so as to release the GIL when doing computationally-intensive tasks such as compression or hashing. Also, the GIL is always released when doing I/O.
Past efforts to create a “free-threaded” interpreter (one which locks shared data at a much finer granularity) have not been successful because performance suffered in the common single-processor case. It is believed that overcoming this performance issue would make the implementation much more complicated and therefore costlier to maintain.
从上文中可以看到,针对GIL的问题做的很多改进,如使用更细粒度的锁机制,在单处理器环境下反而导致了性能的下降。普遍认为,克服这个性能问题会导致CPython实现更加复杂,因此维护成本更加高昂。
However, some extension modules, either standard or third-party, are designed so as to release the GIL when doing computationally-intensive tasks such as compression or hashing. Also, the GIL is always released when doing I/O.
Past efforts to create a “free-threaded” interpreter (one which locks shared data at a much finer granularity) have not been successful because performance suffered in the common single-processor case. It is believed that overcoming this performance issue would make the implementation much more complicated and therefore costlier to maintain.
从上文中可以看到,针对GIL的问题做的很多改进,如使用更细粒度的锁机制,在单处理器环境下反而导致了性能的下降。普遍认为,克服这个性能问题会导致CPython实现更加复杂,因此维护成本更加高昂。
参考资料
1. Python线程,GIL和ctypes: 这是一篇不错的文章,介绍了GIL机制,并给出了利用多核计算能力的其它解决方案,值得一读
2. Python核心编程:第18章多线程编程
0 0
- Python高级特性:全局解释器锁GIL基本概念
- python全局解释器锁(GIL)
- python 什么是全局解释器锁GIL
- python基础-GIL全局解释器锁
- GIL 全局解释器锁
- Python 多线程 多进程 全局解释器锁GIL join
- Python全局解释器锁(Global Interperter Lock, GIL)
- 初识全局解释器锁(GIL)
- 全局解释器锁(GIL)Cpython
- Python3之GIL全局解释器锁
- GIL全局解释器锁(转)
- python GIL 全局锁
- Python 中的 GIL(Global Interpreter Lock) 全局解释器锁是什么?
- 理解python GIL 全局锁
- GIL解释器锁
- 【python】关于python的全局解释锁GIL及线程问题
- Python的全局锁(GIL)问题
- 关于python全局性解释锁(GIL)
- Spring MVC的Controller统一异常处理:HandlerExceptionResolver
- shellinabox : How to access ssh terminal in web browser on Linux Last updated on September 27, 2013
- 图像视频去雾
- bootstrap 下拉菜单
- Codeforces Round #301 (Div. 2)
- Python高级特性:全局解释器锁GIL基本概念
- JDialog的使用
- bootstrap学习笔记之为导航条添加标题、二级菜单及状态
- 黑马程序员_Java学习日记 num1
- bootstrap学习笔记之基础导航条
- 自己在linux上编译、链接、动态库和静态库的学习笔记
- [NOI2009]变换序列(二分图完美匹配)
- ubuntu14.04(X64) qt5.4.0 + vtk6.1.0 Install (安装记录)
- Bootstrap JS插件使用实例(1)-轮播