`
san_yun
  • 浏览: 2597367 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

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

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics