线程安全和全局解释器锁 Thread State and the Global Interpreter Lock

总结: 通过使用GIL后, Python多线程安全, 并且数据保持同步.

Python解释器不是完全线程安全的. 为了支持多线程Python程序, 使用了全局锁(也叫全局解释器锁或GIL), 当前线程在安全的访问Python对象前必须使用它. 如果没有全局锁, 即使最简单的操作都会在多线程程序中引发问题: 例如, 两个进程同时增加同一对象的引用数, 引用数可能只增加了一次而不是二次.

The Python interpreter is not fully thread-safe. In order to support multi-threaded Python programs, there’s a global lock, called the global interpreter lock or GIL, that must be held by the current thread before it can safely access Python objects. Without the lock, even the simplest operations could cause problems in a multi-threaded program: for example, when two threads simultaneously increment the reference count of the same object, the reference count could end up being incremented only once instead of twice.

Therefore, the rule exists that only the thread that has acquired the GIL may operate on Python objects or call Python/C API functions. In order to emulate concurrency of execution, the interpreter regularly tries to switch threads (see sys.setswitchinterval()). The lock is also released around potentially blocking I/O operations like reading or writing a file, so that other Python threads can run in the meantime.

Python3.4 多线程的更多相关文章

  1. Python3之多线程学习

    这里做一个自己复习多线程的笔记 Python中使用线程有两种方式:函数或者用类来包装线程对象. 函数式:调用 _thread 模块中的start_new_thread()函数来产生新线程.语法如下: ...

  2. 【Python3之多线程】

    一.threading模块 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性. 1.开启线程的两种方式(同Process) 方法一 from thr ...

  3. Python3用多线程替代for循环提升程序运行速度

    [本文出自天外归云的博客园] 优化前后新老代码如下: from git_tools.git_tool import get_collect_projects, QQNews_Git from thre ...

  4. python3.4多线程实现同步的四种方式

    临界资源即那些一次只能被一个线程访问的资源,典型例子就是打印机,它一次只能被一个程序用来执行打印功能,因为不能多个线程同时操作,而访问这部分资源的代码通常称之为临界区. 1. 锁机制 threadin ...

  5. python3多线程趣味详解

    python3的多线程很多人无法理解是怎么运行的,因此本文从程序猿的日常生活出发,写了一个由浅入深的多线程教程,这样子大家就不会觉得陌生了,多线程真的很简单很简单! 不要讲多线程局限于库或者框架,自己 ...

  6. python3 多线程编程

    python / 并发 / 线程 / 对象 / 编程   0.什么是线程 1. 多线程模块 2. 创建线程的方法 3. join()方法 4.isAlive()方法 5. name属性和daemon属 ...

  7. Python3 多线程编程 - 学习笔记

    线程 什么是线程 特点 线程与进程的关系 Python3中的多线程 全局解释器锁(GIL) GIL是啥? GIL对Python程序有啥影响? 改善GIL产生的问题 Python3关于多线程的模块 多线 ...

  8. python 之多线程join()

    join()其实就是阻塞线程,控制线程的执行,从而控制住代码的执行顺序. 参照这篇文章:python3对多线程join的理解 通常都是,线程join()后,顺序执行join()后面的代码,如下面的例子 ...

  9. Python socket 广播信息到所有连接的客户端

    Python3,多线程,多客户端,广播数据 #!/usr/bin/env python3 import time import threading import queue import socket ...

随机推荐

  1. 从链接上获取参数值, location.href上获取参数

    /** * 用页面链接上获取参数 * @param {String} name 要获取的参数名 * @return {String} 参数值 */ base.getQueryStringRegExp ...

  2. [013]函数重载--int*和void*的匹配优先级

    同事去面试的时候的问题: 测试一下发现:在同时存在int*和void*的重载函数时,vs2010的环境下,优先匹配void* #include<iostream> using namesp ...

  3. android中设置快捷键方法setShortcut参数的说明

    setShortcut之所以要两个参数来设定两个快捷键是为了应对不同的手机键盘.数字快捷键为12键键盘(0~9,*,#,共12个按键)所准备的.其实我怀疑有这种键盘的手机能装Android么?因为我的 ...

  4. 傲娇Android二三事之操蛋的开发日记(第一回)

    武宗元年 十一月初四 霾 今日魔都,依旧仙雾环绕,仿佛蓬莱落凡尘.望着470这个鲜红的AQI修仙指数,贫道不禁吟道,“正是修仙好光景,雾霾时节又逢君”.但在这个只修bug,不修仙的时代,路上的行人都步 ...

  5. php输出错误信息

    error_reporting(E_ALL);  ini_set('display_errors','on'); header("Content-Type:text/html;charset ...

  6. 将CGPoint、CGSize、CGRect等放进数组的方法

    在oc中,数组中只能存放NSObject类型的数据,所以如果将CGPoint.CGSize.CGRect这些数据存到数组中,我们需要将他们转换为对象类型才可以, 可以借助NSValue,它是用来将基本 ...

  7. C语言里的文件函数

    1.File *pf = fopen("文件名","打开方式"); 文件名可以加路径,两个"\\"或者一个"/" 打开方 ...

  8. JUC全景图

    JUC 并发编程全景图如下:

  9. php入门实现留言板

    首先由一个文本文档read.txt liulan.html <!doctype html> <html lang="en"> <head> &l ...

  10. ASP伪静态页面(不需要通过iis+ISAPI_Rewrite)

    很简单的教程,献给喜欢SEO的朋友们. 不需要通过iis+ISAPI_Rewrite做基于IIS的url rewrite 一.数据库很简单使用ACCESS,Data.mdb建立一个表Article,三 ...