Python线程模块,测试当前线程是否拥有锁

Python线程模块,测试当前线程是否拥有锁,python,multithreading,locking,Python,Multithreading,Locking,我正在阅读Python线程模块,作为类条件的一种方法,如果当前线程拥有锁或不拥有锁,则进行自我测试: def _is_owned(self): # Return Tr

我正在阅读Python线程模块,作为类条件的一种方法,如果当前线程拥有锁或不拥有锁,则进行自我测试:

  def _is_owned(self):                                                                                                                                                               
      # Return True if lock is owned by current_thread.                                                                                                                              
      # This method is called only if __lock doesn't have _is_owned().                                                                                                               
      if self.__lock.acquire(0):                                                                                                                                                     
          self.__lock.release()                                                                                                                                                      
          return False                                                                                                                                                               
      else:                                                                                                                                                                          
          return True  
如果线程a拥有锁,并且线程b尝试测试它是否拥有锁,则此测试将失败,请参见下面的代码:

import thread                                                                                                                                                                            
import time                                                                                                                                                                              

lock = thread.allocate_lock()                                                                                                                                                            
def is_owned(lock):                                                                                                                                                                      
    if lock.acquire(0):                                                                                                                                                                  
        lock.release()                                                                                                                                                                   
        return False                                                                                                                                                                     
    else:                                                                                                                                                                                
        return True                                                                                                                                                                      

def thread1(lock):                                                                                                                                                                       
    lock.acquire()                                                                                                                                                                       
    print 'thread 1 acquired lock, going to block'                                                                                                                                       
    print 'thread 1, lock is owned by me', is_owned(lock)                                                                                                                                
    lock.acquire()                                                                                                                                                                       

def thread2(lock):                                                                                                                                                                       
    time.sleep(5)                                                                                                                                                                        
    print 'thread 2 try to acquire lock, without waiting'                                                                                                                                
    print 'thread 2: lock is owned by me', is_owned(lock)                                                                                                                                

thread.start_new_thread(thread1, (lock,))                                                                                                                                                
thread.start_new_thread(thread2, (lock,))
这些产出是:

In [6]: run is_owned.py
thread 1 acquired lock, going to block
thread 1, lock is owned by me True

In [7]: thread 2 try to acquire lock, without waiting
thread 2: lock is owned by me True
在[7]中,由于sleep5,第二次获取尝试将无限期地阻塞线程


为什么这是一个所有权测试?

它让我想起了一个锁。你好但是您可以从源代码中看到锁是一个普通的线程。lock by allocate_lock这与rlock有什么关系?谢谢你关于有用性的问题或者听起来应该是这样的:为什么这是对线程所有权的测试?我的问题是为什么这是对所有权的测试。谢谢