Global interpreter lock (GIL) is a mechanism used in computer language interpreters to synchronize the execution of

threads so that only one native thread can execute at a time. An interpreter that uses GIL always allows exactly one

thread to execute at a time, even if run on a multi-core processor.

Benefits and drawbacks

increased speed of single-threaded programs (no necessity to acquire or release locks on all data structures separately)

2. easy integration of C libraries that usually are not thread-safe

3. ease of implementation (having a single GIL is much simpler to implement than a lock-free interpreter or one using fine-grained locks).

Use of a global interpreter lock in a language effectively limits the amount of parallelism reachable through concurrency of a

single interpreter process with multiple threads. If the process is almost purely made up of interpreted code and does not

make calls outside of the interpreter for long periods of time (which can release the lock on the GIL on that thread while it

processes), there is likely to be very little increase in speed when running the process on a multiprocessor machine. Due to

signaling with a CPU-bound thread, it can cause a significant slowdown, even on single processors.

为什么要在tornado前面放nginx

原因在于,Tornado采用的的单进程单线程异步IO的网络模型。Python虽然有多线程,但是Python的解释器有GIL这点非常影响了Python

利用多核的能力,所以只能通过多进程来利用多核。既然多进程,一般就需要在前端放置nginx做为负载均衡的反向代理,或是使用这些应

用服务器的wsgi模块来管理进程的生命周期 。

对于含有IO阻塞的环境,比如网络通讯、磁盘读写, 当发生阻塞时,CPU是闲置的,此时如果就一个线程就没法处理其他事情了。

所以多线程可以提高CPU利用率。

 
 

参考:线程安全及Python中的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. **解释器全局锁(Global Interpreter Lock)

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

  3. Python GIL(Global Interpreter Lock)

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

  4. Python3 GIL(Global Interpreter Lock)与多线程

    GIL(Global Interpreter Lock)与多线程 GIL介绍 GIL与Lock GIL与多线程 多线程性能测试 在Cpython解释器中,同一个进程下开启的多线程,同一时刻只能有一个线 ...

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

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

  6. Python GIL(Global Interpreter Lock)

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

  7. python GIL(Global Interpreter Lock)

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

  8. 基于Cpython的 GIL(Global Interpreter Lock)

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

  9. Python解释器是单线程应用 IO 密集型 计算密集型 GIL global interpreter lock

    [Python解释器是单线程应用] [任意时刻,仅执行一个线程] 尽管Python解释器中可以运行多个线程,但是在任意给定的时刻只有一个线程会被解释器执行. [GIL锁 保证同时只有一个线程运行] 对 ...

随机推荐

  1. java 读取Zip文件进行写入

    直接读取ZIp文件读取写入到别的文件中. package jp.co.misumi.mdm.batch; import java.io.BufferedReader; import java.io.F ...

  2. UML类图<转>

    UML类图     解释UML类图: 1.       首先看“动物”矩形框,它代表一个类.该类图分为三层,第一层显示类的名称,如果是抽象类就要用斜体显示.第二层是类的特性,通常就是字段和属性.第三层 ...

  3. 示例 - 如何在多线程中应用SpiderStudio生成的DLL?

    >> 接上文 "示例 - 如何在Console应用程序中应用SpiderStudio生成的DLL?", 将其改成多线程: 代码: using System; using ...

  4. NameNode机制和DataNode机制

    首先我们看一下NAMENODE: 我们已经知道了NAMENODE作为DATANODE的管理者,其重要性不言而喻,那么NAMENODE是怎么管理数据的呢? 首先,我们看一下上面这张图,每次客户端读写数据 ...

  5. C语言中对输入输出格式的控制

    格式化输出的控制 #include<stdio.h> int main(void){ float a=111123.681111f; printf("%1.3f",a) ...

  6. jQuery开发中容易忽视的错误

    1.引用jQuery库文件的<script>标签,必须放在引用自定义脚本文件的<script>标签之前,否则,就会发生找不到对象:最好在<head>元素中,把引入样 ...

  7. C语言错误: CRT detected that the application wrote to memory after end of heap buffer

    CRT detected that the application wrote to memory after end of heap buffer 多是中间对其进行了一些操作,在程序结束处,释放内存 ...

  8. 第二百七十九节,MySQL数据库-pymysql模块操作数据库

    MySQL数据库-pymysql模块操作数据库 pymysql模块是python操作数据库的一个模块 connect()创建数据库链接,参数是连接数据库需要的连接参数使用方式: 模块名称.connec ...

  9. 浅谈Unity中的GC以及优化

    介绍: 在游戏运行的时候,数据主要存储在内存中,当游戏的数据不在需要的时候,存储当前数据的内存就可以被回收再次使用.内存垃圾是指当前废弃数据所占用的内存,垃圾回收(GC)是指将废弃的内存重新回收再次使 ...

  10. ThinkPHP项目笔记之RBAC(权限)下篇

    接着谈谈:添加用户以及用户管理列表 e.添加用户