参考文档:
http://www.rubyinside.com/does-the-gil-make-your-ruby-code-thread-safe-6051.html
http://www.jstorimer.com/blogs/workingwithcode/8085491-nobody-understands-the-gil
http://www.jstorimer.com/blogs/workingwithcode/8100871-nobody-understands-the-gil-part-2-implementation
http://stackoverflow.com/questions/2227169/are-python-built-in-containers-thread-safe
http://jessenoller.com/blog/2009/02/01/python-threads-and-the-global-interpreter-lock
https://wiki.python.org/moin/GlobalInterpreterLock
上面的TODO不是为了做thread safe的, add ability to block waiting on a connection to be released
,个人理解是connection在release的时候,其他方法被block。但是他的实现我没看懂,os.getpid()不是获取进程id吗?
pool正常调用顺序,get_conneciton(),release()
假设Thread A,B,那么可能出现的随机组合:(A.get_con,B.release),(A.get_con,B.get_con),(A.release,B.get_con),(A.release,B.release)
thread pool的thread safe关键点是:已经被线程A标记为使用的connection不能被B线程使用到。
如果能做到上面这点可以认为thread pool是线程安全的。
def get_connection(self):
try:
connection = self._available_connections.pop()
except IndexError:
connection = self._make_connection()
if connection and connection.connect():
self._in_use_connections.add(connection)
return connection
else:
return None
def _make_connection(self):
"Create a new connection"
if self._created_connections >= self._max_connections:
raise DbossError("Too many connections")
conn = Connection(self,self.location,self.timeout)
self._created_connections += 1
return conn
_available_connection有元素的情况:
如果_available_connection.pop()被线程A调用后,被线程B调用,线程B是否会得到和线程A一样的connection?这个是python的set来保证,据我所知,set是保证。
_available_connection没有元素的情况:
如果_available_connection.pop()被线程A调用后,被线程B调用,这个时候会触发IndexError,进入_make_connection。这里有两种情况:
1. 线程A已经执行完_make_connection(),线程B调用self._available_connections.pop()。
2. 线程A在_make_connection()执行过程中,线程B调用self._available_connections.pop()。
可以发现上述两种情况线程B都会触发IndexError异常,对结果没有影响。
相关推荐
Python3资源 Python3资源
Gil-Web-Cache-Deception-Attack
us-17-Gil-Web-Cache-Deception-Attack.pdf
Gil-Web-Cache-Deception-Attack-wp
us-17-Gil-Web-Cache-Deception-Attack-wp.pdf
理解python中GIL对并发的影响以及如何更好的并发
Python的GIL会对CPU密集型的程序产生影响,如果完全使用Python来编程,怎么避开GIL的限制呢?
python GIL 详细说明 由python cookbook作者编写
Python语言的全局解释锁GIL详解,由David Beazley写的文档,分析了单核CPU、多核CPU下的Python多线程的效率,并分析线程调度,CPU竞争,以及在NEW GIL等内容,相信看完后会对Python的GIL有一个深入的理解。
详细解析了Python GIL的实现和存在的问题
《再谈Python的GIL》一文件用到的例子,放在这里,方便大家下载测试,加深对GIL的理解
比较详细的介绍了全局解释锁GIL的实现原理过程,全英文的
资源分类:Python库 所属语言:Python 资源全名:gil_galad-0.1-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
个人收集的一些关于Python的相关面试知识点和题目,包含问答题、编程题等,适用于Python初学者、Python爱好者等。 部分目录如下: 1.标准数据类型? 2 2.如何创建一个字典?...22.谈下python的GIL? 7 23. ......
python-3.2rc3 release candidate 3 更新内容: numerous improvements to the unittest module PEP 3147, support for .pyc repository directories PEP 3149, support for version tagged dynamic libraries ...
Python_threading_5_不一定有效率_GIL_(多线程_教学教程tutorial)
GIL的全称为Global Interpreter Lock,全局解释器锁。本篇文章详细的介绍了Python中GIL的使用,有需要了解Python中GIL用法的朋友可参考。希望此文章对各位有所帮助
python面试常见问题1、“==”与is的区别2、list与tuple的区别3、python中list与dict4、局部变量和全局变量5、迭代器和生成器6、yield7、import过程8、python装饰器9、python特点(封装、继承、多态)10、垃圾回收...
而Python为很多人所抱怨的一点就是GIL,那么python为什么选择使用GIL, 本文也就这个问题进行一些讨论. 引入 你的PC或者笔记本还是单核吗? 如果是,那你已经out了. 随着纳米技术的不断进步, 计算机芯片的工艺也在进步,...