线程安全和全局解释器锁 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. NET中間語言(IL) 图解

    转载地址是:http://msdn.microsoft.com/zh-tw/library/dd229210.aspx 想查看IL指令,请看中英文对照表: CN-http://www.cnblogs. ...

  2. com.service.impl

    package com.service.impl; import java.util.ArrayList; import java.util.LinkedHashMap; import java.ut ...

  3. java_jdbc_基本连接池

    MyDataSource实现封装连接池,MyConnection实现connection类,通过代理模式相互调用 package cn.itcast; import java.sql.*; publi ...

  4. 【面试题】如何让C语言自动发现泄漏的内存

    1. 题目 改造malloc和free函数,使C语言能自动发现泄漏的内存,在程序退出时打印中遗漏的内存地址和大小. 2. 思路 用一个链表来记录已经分配的内存地址.在malloc时,把分配的内存地址和 ...

  5. Apache rewrite 详解

    用rewrite可实现的部分:URL根目录搬迁,多目录查找资源,阻止盗连你的图片,拒绝某些主机访问,基于时间重写,据浏览器类型重写,动态镜像远程资源,外部重写程序模板,等等 详见下表: 目标 重写设置 ...

  6. ant例子

    1.安装ant 下载解压→环境变量配置→cmd输入ant 出现 Buildfile: build.xml does not exist! 代表安装成功 参考文章:http://www.cnblogs. ...

  7. IIS 之 HTTP错误信息提示

    一.HTTP返回码 [1]1xx - 信息提示 这些状态代码表示临时的响应.客户端在收到常规响应之前,应准备接收一个或多个 1xx 响应. a. 100 - 继续. b. 101 - 切换协议. [2 ...

  8. RadioGroup&RadioButton

    RadioGroup提供多选一机制:属性orientation:“vertlcal” or “horizontal” @Override public void onCheckedChanged(Ra ...

  9. Hibernate学习笔记--------2.一多|多多的CRUD

    一.一多关系 例如用户(Tb_User)和订单(Tb_Order)之间,一个用户对应了多个订单,多个订单对应一个用户. 除了基本的配置外,需要在用户类(单方)中添加订单的集合同样需要get/set方法 ...

  10. lshw 命令(查看硬件信息)

    帮助 $ lshw -h Hardware Lister (lshw) - B.02.16 usage: lshw [-format] [-options ...] lshw -version -ve ...