Threading Module源码概述(一)
Python的Threading模块是建立在thread module基础上的一个模块,在threading模块中,暴露着许多thread模块的属性。比如threading._get_ident实际上就是thread.get_ident.
_start_new_thread = thread.start_new_thread
_allocate_lock = thread.allocate_lock
_get_ident = thread.get_ident
ThreadError = thread.error
在threading模块中,有一套记录当前所有通过继承threading.Thread而创建的python线程的机制。这个机制通过两个dict 和一个lock完成。
# Active thread administration
_active_limbo_lock = _allocate_lock()
_active = {} # maps thread id to Thread object
_limbo = {}
threading.py
我们知道通过threading.Thread创建多线程有两个阶段。
第一个阶段调用threading.Thread.start
第二个阶段是在threading.Thread.start中调用threading.Thread.run。
当处于第一阶段时,还没有调用thread.start_new_thread创建原生子线程,这时候把线程记录在了_limbo中。由于没有创建子线程,所以现在没有线程id,记录方式为 _limbo[self] = self。在第二阶段,已经成功地调用thread.start_new_thread创建原生子线程,这时将从_limbo中删除子线程,而将子线程记录到_active中,记录方式为_active[self.__ident] = self 或者_active[thread_id] = thread。可见,这Python这两个dict分别维护了自己已经创建和等待创建的子线程集合。对这两个dict的访问都在_active_limbo_lock的保护之下进行。
def start(self):
with _active_limbo_lock:
_limbo[self] = self #将线程添加到dict中
第一步
def start(self):
try: #调用_start_new_thread创建子线程
_start_new_thread(self.__bootstrap, ()) def __bootstrap(self):
try:
self.__bootstrap_inner() def __bootstrap_inner(self):
try:
self._set_ident() #获得线程id
self.__started.set()
with _active_limbo_lock:
_active[self.__ident] = self #将线程id保存到_active的dict
del _limbo[self] #移除之前limbo字典中的线程
第二步
Threading Module源码概述(一)的更多相关文章
- Threading Module源码概述(二)
在threading 模块中,提供了列举当前所有子线程的操作.threading.enumerate.这个操作很简单,就是将_active和_limbo中维护的线程集合的信息输出. def enume ...
- Threading Module源码概述(三)
Threading中的Thread threading中一个关键的组件是threading.Thread. class Thread(_Verbose): __initialized = False ...
- MongoDB源码概述——内存管理和存储引擎
原文地址:http://creator.cnblogs.com/ 数据存储: 之前在介绍Journal的时候有说到为什么MongoDB会先把数据放入内存,而不是直接持久化到数据库存储文件,这与Mong ...
- Kakfa揭秘 Day3 Kafka源码概述
Kakfa揭秘 Day3 Kafka源码概述 今天开始进入Kafka的源码,本次学习基于最新的0.10.0版本进行.由于之前在学习Spark过程中积累了很多的经验和思想,这些在kafka上是通用的. ...
- 安卓图表引擎AChartEngine(二) - 示例源码概述和分析
首先看一下示例中类之间的关系: 1. ChartDemo这个类是整个应用程序的入口,运行之后的效果显示一个list. 2. IDemoChart接口,这个接口定义了三个方法, getName()返回值 ...
- python线程threading.Timer源码解读
threading.Timer的作用 官方给的定义是: """Call a function after a specified number of seconds: t ...
- threading.local()源码分析
前段时间写了个多线程的程序,了解到Python中有个与众不同的thread.local()方法,可以创建一个全局对象,各个线程可以用这个全局对象保存各自的局部变量,而在使用时不受其他线程的影响.于是抽 ...
- elasticsearch 源码概述
从功能上说,可以分为两部分,分布式功能和数据功能.分布式功能主要是节点集群及集群附属功能如restful借口.集群性能检测功能等,数据功能主要是索引和搜索.代码上这些功能并不是完全独立,而是由相互交叉 ...
- python语言线程标准库threading.local源码解读
本段源码可以学习的地方: 1. 考虑到效率问题,可以通过上下文的机制,在属性被访问的时候临时构建: 2. 可以重写一些魔术方法,比如 __new__ 方法,在调用 object.__new__(cls ...
随机推荐
- SQL存储过程基于字段名传入的字符串拼接.
--定义存储过程. Create PROCEDURE Usp_Static ), ), --分组字段. ), --统计字段. ), --表头字段. ) --聚会函数. AS ) --存储游标执行的列. ...
- 关于jquery的 $("form").serialize()和 new FormData表单序列化
$("form").serialize()和 new FormData($('#uploadForm')[0])都是序列化表单,实现表单的异步提交,但是二者有区别 首先,前者,只能 ...
- oracle-绑定变量学习笔记(未完待续)
--定义变量SQL> var a number; --给绑定变量赋值SQL> exec :a :=123; PL/SQL procedure successfully completed. ...
- UITableViewCell 添加长按手势
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self actio ...
- hdoj 2049 错排
代码: #include <stdio.h> int main(){ int n,a,b,i,j; __int64 s[22],h[22]; s[1]=0; s[2]=1; s[3]=2; ...
- python 数据结构
Python的数据结构主要分为set(),list[],和dict{}.这篇文章主要记载这几种结果在使用过程中的一些技巧或其中一些函数的用法区别. 1.函数get()与setdefault()区别: ...
- SGU 197.Nice Patterns Strike Back
时间限制:0.5s 空间限制:6M 题意: 给出长n(n<=10^100)和宽m(m<=5)的地面,铺上黑色和白色的地板,使得没有任意一个2*2大小的地面铺同种颜色的方案数是多少. Sol ...
- [转]iframe自适应宽度高度
<iframe id="iframe" onLoad="AutoFit();" frameborder="0" scrolling=& ...
- yii2源码学习笔记(五)
Event是所有事件类的基类.它封装了与事件相关的参数. yii2\base\Event.php <?php /** * @link http://www.yiiframework.com/ * ...
- int*-------int
a=(int)((int*)0 + 4)求a是多少 大家看图应该明白了 十六进制0x00000010转换为十进制就是16