关于网站开发的网店计划书范文,网络黄页进入有限公司,网页设计和制作的三大步骤,微信网站后台什么是全局解释器锁GIL Python代码的执行由Python 虚拟机(也叫解释器主循环#xff0c;CPython版本)来控制#xff0c;Python 在设计之初就考虑到要在解释器的主循环中#xff0c;同时只有一个线程在执行#xff0c;即在任意时刻#xff0c;只有一个线程在解释器中运行。对… 什么是全局解释器锁GIL Python代码的执行由Python 虚拟机(也叫解释器主循环CPython版本)来控制Python 在设计之初就考虑到要在解释器的主循环中同时只有一个线程在执行即在任意时刻只有一个线程在解释器中运行。对Python 虚拟机的访问由全局解释器锁GIL来控制正是这个锁能保证同一时刻只有一个线程在运行。 在多线程环境中Python 虚拟机按以下方式执行 1. 设置GIL 2. 切换到一个线程去运行 3. 运行 a. 指定数量的字节码指令或者 b. 线程主动让出控制可以调用time.sleep(0) 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实现更加复杂因此维护成本更加高昂。