【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. 使用OPENROWSET爆破SQL Server密码

    使用OPENROWSET爆破SQL Server密码   OPENROWSET函数是SQL Server提供的一个连接函数.它可以用于使用OLE DB方式连接一个数据库,并进行数据查询等操作.使用该函 ...

  2. RxJava Android(RxAndroid) 开发全家桶

    RxJava 在 Android 应用开发中越来越流行,但是由于其门槛稍高,初次使用不免遇到很多问题,例如在 RxJava 常见的错误用法 和 不该使用 RxJava 的一些情况 中所描述的情况.为了 ...

  3. EasyMvc入门教程-高级控件说明(20)表格控件

    表单与表格是信息化系统里很常见的控件,EasyMvc提供了简单的数据绑定方式(基于Json),看下面的示例:  准备的接口地址代码如下:(该接口适用以下所有例子) public IActionResu ...

  4. 第十二题 Merge Sorted Array

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume th ...

  5. AngularJS中,<span class="bluetext" ng-bind="ctrl.user.name|uppercase"></span>和{{ctrl.user.name|uppercase}}是等价的,但不等于<span class="bluetext" ng-bind="ctrl.user.name|uppercase"/>

    代码下载:https://files.cnblogs.com/files/xiandedanteng/angularjsAttenSpan.rar AngularJS中,<span class= ...

  6. 一群牛人翻译:The Swift Programming Language 中文版

    无聊闲逛GIthub,看到一群牛人在github上创建了一个关于Switf的文档翻译项目 The Swift Programming Language 中文版 项目地址:中文版 Apple 官方 Sw ...

  7. make -j 4 echo !$

    make -j 4 #以cpu四核编译 !$上一次命令空格后的部分

  8. apue学习笔记(第十五章 进程间通信)

    本章将说明进程之间相互通信的其它技术----进程间通信(IPC) 管道 管道只能在具有公共祖先的两个进程之间只用.通常,一个管道由一个进程创建,在进程调用fork后,这个管道就能在父进程和子进程之间使 ...

  9. struts2实现文件查看、下载

      CreateTime--2017年9月7日10:25:33 Author:Marydon struts2实现文件查看.下载 1.界面展示 <a style="color: #199 ...

  10. C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志

    C#实现多级子目录Zip压缩解压实例 参考 https://blog.csdn.net/lki_suidongdong/article/details/20942977 重点: 实现多级子目录的压缩, ...