python GIL-并发问题小结

参考文档:

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异常,对结果没有影响。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值