GIL - global interpreter lock
python是一个解释型语言,但是可以使用多个解释器。比如C++,但是可以用不同的编译器来编译成可执行代码。有名的编译器例如GCC,INTEL C++,Visual C++等。Python也一样,同样一段代码可以通过CPython,PyPy,Psyco等不同的Python执行环境来执行。像其中的JPython就没有GIL。然而因为CPython是大部分环境下默认的Python执行环境。所以在很多人的概念里CPython就是Python,也就想当然的把GIL归结为Python语言的缺陷。所以这里要先明确一点:GIL并不是Python的特性,Python完全可以不依赖于GIL。
Cpython中包含一个GIL , 全局解释锁。设计之初是因为防止在解释器的主循环中,只有一个线程在运行。
但是计算机的能力提升,cpu多核的产生,python也开始支持多线程编程,但是线程的安全就是要加锁。于是有了GIL的超级大锁。但是GIL不是线程安全的。这就造成执行效率低的结果。
官方解释:
In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native threads from executing Python bytecodes at once. This lock is necessary mainly because CPython’s memory management is not thread-safe. (However, since the GIL exists, other features have grown to depend on the guarantees that it enforces.)
翻译:
在CPython中,全局解释器锁(global interpreter lock, GIL)是一个互斥体,它防止多个本机线程同时执行Python字节码。这个锁是必要的,主要是因为CPython的内存管理不是线程安全的。(然而,自从GIL存在以来,其他特性已经逐渐依赖于它强制执行的保证。)
GIL无疑就是一把全局排他锁。毫无疑问全局锁的存在会对多线程的效率有不小影响。甚至就几乎等于Python是个单线程的程序。
怎么解决呢?
用multiprocess替代Thread
multiprocess库的出现很大程度上是为了弥补thread库因为GIL而低效的缺陷。它完整的复制了一套thread所提供的接口方便迁移。唯一的不同就是它使用了多进程而不是多线程。每个进程有自己的独立的GIL,因此也不会出现进程之间的GIL争抢。
参考:
https://www.cnblogs.com/SuKiWX/p/8804974.html
GIL - global interpreter lock的更多相关文章
- Python3 GIL(Global Interpreter Lock)与多线程
GIL(Global Interpreter Lock)与多线程 GIL介绍 GIL与Lock GIL与多线程 多线程性能测试 在Cpython解释器中,同一个进程下开启的多线程,同一时刻只能有一个线 ...
- python之GIL(Global Interpreter Lock)
一 介绍 ''' 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple nati ...
- python GIL(Global Interpreter Lock)
一 介绍 ''' 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple nati ...
- 基于Cpython的 GIL(Global Interpreter Lock)
一 介绍 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native t ...
- Python解释器是单线程应用 IO 密集型 计算密集型 GIL global interpreter lock
[Python解释器是单线程应用] [任意时刻,仅执行一个线程] 尽管Python解释器中可以运行多个线程,但是在任意给定的时刻只有一个线程会被解释器执行. [GIL锁 保证同时只有一个线程运行] 对 ...
- Python GIL(Global Interpreter Lock)
一,介绍 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native t ...
- python之GIL官方文档 global interpreter lock 全局解释器锁
0.目录 2. 术语 global interpreter lock 全局解释器锁3. C-API 还有更多没有仔细看4. 定期切换线程5. wiki.python6. python.doc FAQ ...
- Python GIL(Global Interpreter Lock)
一.介绍 In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native threa ...
- 理解Global interpreter lock
Global interpreter lock (GIL) is a mechanism used in computer language interpreters to synchronize ...
随机推荐
- Charles重发请求
1.如下图 2.选中某个接口,右键--选择 Repeat Advanced选项,设置请求多次 3.
- 面试题22:链表中倒数第k个节点
# -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val = x # self.next = None ...
- python 查看以及更新安装包
查看 在终端(windows:电脑win+R, linux:ctrl+alt+T)输入: pip list 或者 conda list 更新 在终端(windows:电脑win+R, linux:ct ...
- <JAVA - 大作业(1)文本编辑器 >
<JAVA - 大作业(1)文本编辑器 > 背景 JAVA上机大作业:qq / 代码评价系统 第一次上机主题是练习JAVA自带的GUI图形化编程 目的:实现一个跟window10记事本界面 ...
- 怎么学习PHP
学习PHP有半个月了.每天都要打代码and写笔记.学过C和Java,在学习PHP的过程中比较顺利吧 (^-^) 代码打得越多,运行得越多,慢慢得会对程序理解得越深.下面就讲讲我学习PHP的心得.PHP ...
- Goals ? Ideals ?
Why is it important to set goals ? Because goal can help you do , be , and experience anything you w ...
- SqlServer表名称定义
每一个数据表 添加一个 扩展 属性:Description 填写表描述. 查看是否所有表都添加的Sql如下: SELECT a.name AS name, g.[value] FROM sys.ta ...
- JavaScript 盖尔-沙普利算法
最近在学 JavaScript , 为了尽快熟悉语法,决定移植以前写的盖尔-沙普利算法. c# 下的代码:https://www.cnblogs.com/aitong/p/10973774.html ...
- 【Tensorflow】slim.arg_scope()的使用
https://blog.csdn.net/u013921430/article/details/80915696
- String 字符串和StringBuffer的知识点总结
String字符串 1 字符串.equals(); equals和length的区别:equals ...