Python中RLock的源码实现问题解析

threading.py 中,默认调用的是 _CRLock, Python 也有自己实现的_PyRLock,我测试中将默认的改为了_PyRLock。

为了测试,就单独创建了一个 RLock 对象。

lock = threading.RLock()
print(lock)

RLock 中的 acquire 源码如下:

print('Start calling acquire: owner={}, count={}'.format(self._owner, self._count))
me = get_ident()
if self._owner == me:
    self._count += 1
    print('(Counter)End calling acquire: owner={}, count={}'.format(self._owner, self._count))
    return 1
 rc = self._block.acquire(blocking, timeout)
if rc:
    print('Get the lock')
    self._owner = me
    self._count = 1
print('End calling acquire: owner={}, count={}'.format(self._owner, self._count))
return rc

print 语句是我自己添加的。 执行最上面两行后,结果如下:

Start calling acquire: owner=None, count=0
Get the lock
End calling acquire: owner=140736157979584, count=1
Start calling acquire: owner=None, count=0
Get the lock
End calling acquire: owner=140736157979584, count=1
<unlocked threading._RLock object owner=None count=0 at 0x104b7b780>
Start calling acquire: owner=None, count=0
Get the lock
End calling acquire: owner=140736157979584, count=1
Start calling acquire: owner=140736157979584, count=1
(Counter)End calling acquire: owner=140736157979584, count=2
Start calling acquire: owner=None, count=0
Get the lock
End calling acquire: owner=140736157979584, count=1

不太理解为何创建对象后,结果在 <unlocked threading._RLock object owner=None count=0 at 0x104b7b780> 这句话后,还会进行计数器的变动和获取锁的过程,而且我创建的时候也没有直接调用 acquire 方法进行获取锁。

希望了解的大神能解释一下,谢谢


Python中RLock的源码实现问题解析

1 回复

我看了下Python标准库threading.py里RLock的源码实现,核心逻辑其实挺清晰的。

RLock(可重入锁)的关键是维护了_owner_count两个属性。_owner记录当前持有锁的线程ID,_count记录重入次数。源码里获取锁的主要逻辑是这样的:

def acquire(self, blocking=True, timeout=-1):
    me = get_ident()  # 获取当前线程ID
    if self._owner == me:
        # 如果是当前线程已经持有锁,就增加重入计数
        self._count += 1
        return 1
    
    # 否则尝试获取底层锁
    rc = self._block.acquire(blocking, timeout)
    if rc:
        # 获取成功,设置owner和计数
        self._owner = me
        self._count = 1
    return rc

释放锁的逻辑对应着:

def release(self):
    if self._owner != get_ident():
        raise RuntimeError("cannot release un-acquired lock")
    
    self._count -= 1
    if not self._count:
        # 计数归零时才真正释放底层锁
        self._owner = None
        self._block.release()

这里有个细节:RLock内部其实包装了一个普通的Lock(_block),真正的锁竞争发生在这个底层Lock上。重入机制完全由Python层通过_owner_count来管理,这避免了内核态的频繁切换。

源码里还处理了线程终止的情况,_is_owned()方法用来检查当前线程是否持有锁。整体实现比较简洁,就是通过计数和线程ID判断来实现重入特性。

理解RLock的关键是明白它只是在Lock上加了一层重入逻辑的包装。

回到顶部