2016-11-12 213 views
0

如何结合threading.RLockthreading.Semaphore?还是这样的结构已经存在?Python:构建可重入信号量(结合RLock和信号量)

在Python中,有一个可重入锁的原语,threading.RLock(N),它允许同一个线程多次获取一个锁,但是没有其他线程可以。还有threading.Semaphore(N),它允许在锁定之前获得锁定N次。如何结合这两种结构?我想最多N单独的线程能够获得锁,但我希望线程上的每个单独的锁是一个可重入的线程。

回答

0

所以我猜一个可重入信号量不存在。这是我提出的实现,很乐意接受评论。

import threading 
import datetime 
class ReentrantSemaphore(object): 
    '''A counting Semaphore which allows threads to reenter.''' 
    def __init__(self, value = 1): 
    self.local = threading.local() 
    self.sem = threading.Semaphore(value) 

    def acquire(self): 
    if not getattr(self.local, 'lock_level', 0): 
     # We do not yet have the lock, acquire it. 
     start = datetime.datetime.utcnow() 
     self.sem.acquire() 
     end = datetime.datetime.utcnow() 
     if end - start > datetime.timedelta(seconds = 3): 
     logging.info("Took %d Sec to lock."%((end - start).total_seconds())) 
     self.local.lock_time = end 
     self.local.lock_level = 1 
    else: 
     # We already have the lock, just increment it due to the recursive call. 
     self.local.lock_level += 1 

    def release(self): 
    if getattr(self.local, 'lock_level', 0) < 1: 
     raise Exception("Trying to release a released lock.") 

    self.local.lock_level -= 1 
    if self.local.lock_level == 0: 
     self.sem.release() 

    __enter__ = acquire 
    def __exit__(self, t, v, tb): 
    self.release()