【Python解释器是单线程应用】

【任意时刻,仅执行一个线程】

尽管Python解释器中可以运行多个线程,但是在任意给定的时刻只有一个线程会被解释器执行。

【GIL锁 保证同时只有一个线程运行】

对Python虚拟机的访问是由全局解释器锁(GIL)控制的。这个锁就是用来保证同时只有一个线程运行的。

【Python虚拟机】

Python代码的执行是由Python虚拟机(又名解释器主循环)进行控制的。在主循环中同时只能有一个控制线程在执行。

在内存中可以有许多程序,但是在任意给定时刻只能有一个程序在运行。

在多线程环境中,Python虚拟机将按照下面所述方式执行:

1、设置GIL;

2、切换一个线程去运行;

3、执行下面操作之一:

  a.指定数量的字节码指令;

  b.线程主动让出控制权;

4、把线程设置回睡眠状态(切换出线程)。

5、解锁GIL;

6、重复上述步骤

https://wiki.python.org/moin/GlobalInterpreterLock

In CPython, the global interpreter lock, or GIL, is a mutex that protects access to Python objects, preventing multiple 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 extensions must be GIL-aware in order to avoid defeating threads. For an explanation, see Global interpreter lock.

【阻碍多核】

The GIL is controversial because it prevents multithreaded CPython programs from taking full advantage of multiprocessor systems in certain situations. Note that potentially blocking or long-running operations, such as I/O, image processing, and NumPy number crunching, happen outside the GIL. Therefore it is only in multithreaded programs that spend a lot of time inside the GIL, interpreting CPython bytecode, that the GIL becomes a bottleneck.

However the GIL can degrade performance even when it is not a bottleneck. Summarizing those slides: The system call overhead is significant, especially on multicore hardware. Two threads calling a function may take twice as much time as a single thread calling the function twice. The GIL can cause I/O-bound threads to be scheduled ahead of CPU-bound threads. And it prevents signals from being delivered.

Python解释器是单线程应用 IO 密集型 计算密集型 GIL global interpreter lock的更多相关文章

  1. python之GIL(Global Interpreter Lock)

    一 介绍 ''' 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple nati ...

  2. python GIL(Global Interpreter Lock)

    一 介绍 ''' 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple nati ...

  3. python之GIL官方文档 global interpreter lock 全局解释器锁

    0.目录 2. 术语 global interpreter lock 全局解释器锁3. C-API 还有更多没有仔细看4. 定期切换线程5. wiki.python6. python.doc FAQ ...

  4. Python GIL(Global Interpreter Lock)

    一,介绍 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native t ...

  5. Python GIL(Global Interpreter Lock)

    一.介绍 In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native threa ...

  6. **解释器全局锁(Global Interpreter Lock)

    解释器全局锁(Global Interpreter Lock),即Python为了保证线程安全而采取的独立线程运行的限制,说白了就是一个核只能在同一时间运行一个线程. [解决办法就是多进程和协程(协程 ...

  7. IO密集型 计算密集型

    参考:https://www.cnblogs.com/zhangyux/p/6195860.html 参考:廖雪峰 协程 gevent IO密集型任务指的是磁盘IO或者网络IO占主要的任务,计算量很小 ...

  8. IO密集型和计算密集型

    我们常说的多任务或者单任务分为两种: IO密集型的任务  计算密集型的任务   IO密集型的任务或:有阻塞的状态,就是不一直会运行CPU(中间就一个等待状态,就告诉CPU 等待状态,这个就叫IO密集型 ...

  9. 浅谈Java两种并发类型——计算密集型与IO密集型

    转载:https://blog.csdn.net/u013070853/article/details/49304099 核心是可以分别独立运行程序指令的计算单元.线程是操作系统能够进行运算调度的最小 ...

随机推荐

  1. luogu P1336 最佳课题选择

    题目描述 Matrix67要在下个月交给老师n篇论文,论文的内容可以从m个课题中选择.由于课题数有限,Matrix67不得不重复选择一些课题.完成不同课题的论文所花的时间不同.具体地说,对于某个课题i ...

  2. [TJOI2016][HEOI2016]排序

    题目大意: 给定一个$1\sim n(n\leq10^5)$的全排列,有$m(m\leq10^5)$次操作,每次把区间$[l,r]$按照升序或降序排序.最后询问所有操作完成后,位置为$q$的数是多少. ...

  3. 谷歌浏览器chrome设置特定网页使用Https(ssl)访问

    1.在浏览器上输入: chrome://net-internals/ 2.在以下位置输入要自动进行https访问的域名即可. 注意:域名不能用范解析,只能一个一个手动输入.

  4. php设置报错级别

    ini_set("display_errors", "On");//若页面不报错的话,请设置php.ini 的display_errors 为 On error ...

  5. Android性能优化第(二)篇---Memory Monitor检测内存泄露

    上篇说了一些性能优化的理论部分,主要是回顾一下,有了理论,小平同志又讲了,实践是检验真理的唯一标准,对于内存泄露的问题,现在通过Android Studio自带工具Memory Monitor 检测出 ...

  6. htop简介

    htop可谓top的升级版,top不可以鼠标操作,但是htop可以使用鼠标操作 启动方式:命令行下输入htop直接启动,启动后的界面如图所示 操作:可以使用上下左右方向键进行移动查看.可以使用鼠标点击 ...

  7. Asp.net对文件夹和文件的操作类

    using System; using System.IO; using System.Web; namespace SEC { /**//// /// 对文件和文件夹的操作类 /// public ...

  8. RTC实时时钟驱动

    RTC(Real-Time Clock)实时时钟为操作系统提供了一个可靠的时间,并且在断电的情况下,RTC实时时钟也可以通过电池供电,一直运行下去. RTC通过STRB/LDRB这两个ARM指令向CP ...

  9. 搭建k8s集群的手顺

    https://www.cnblogs.com/netsa/category/1137187.html

  10. 【DQ冰淇淋】—— Babylon 冰淇淋三维互动营销项目总结

    前言:在学习过Babylon.js基础之后,我上手的第一个网页端3D效果制作项目就是‘DQ冰淇淋’.这个小项目应用到了Babylon最基础的知识,既可以选味道,选点心,也可以旋转.倒置冰淇淋,互动起来 ...