.locked() returns 即使在 python 多线程环境中调用 release() 之后也是如此
.locked() returns True even after release() called in python multithreading environment
我正在尝试使用 python 线程编写我的第一个代码。
请看代码
当我使用 release() 为线程释放锁时出现问题,因为它说锁仍然可用 [locked() returns True even after release()]
import threading
import time
class thread (threading.Thread):
def __init__(self,t,d,arr,lock):
threading.Thread.__init__(self)
self.name=t
self.delay=d
self.array=arr;
self.lock=lock
def run(self):
self.fun1()
def fun1(self):
print "the thread %s want to take a lock now" %self.name
self.lock.acquire()
print "lock status just after lock acquire foe",self.name,self.lock.locked()
time.sleep(self.delay)
print "release lock for %s" %self.name
self.lock.release()
time.sleep(2)
print "lock status after lock release is",self.name,self.lock.locked()
lck=threading.Lock()
obj1=thread('T1',5,[1,2,3],lck)
obj2=thread('T2',10,[4,5,6],lck)
obj1.start()
obj2.start()
输出
=====
the thread T1 want to take a lock now
the thread T2 want to take a lock now
lock status just after lock acquire foe T2 True
release lock for T2
lock status just after lock acquire foe T1 True
lock status after lock release is T2 True
release lock for T1
lock status after lock release is T1 False
我的问题是:
线程 T2 首先获得锁并执行它的 space。
我们可以使用 .locked() 看到 T2 的锁定状态是“
之后的锁定状态”
lock acquire foe T2 True”。所以 T2 现在得到了锁。
一旦 T2 使用 release() 释放了锁,那么 T1 也按预期获得了锁
但是在使用 release() 释放 T2 的锁后,locked() 显示为 True。这意味着 T2 仍然可以锁定?这种情况下T1是怎么获取到锁的?。一旦 T1 执行并在 release() 之后,我可以看到 .locked() returns False。意味着对于 T1 locked() 一旦释放锁就按预期工作,立即 return False
简而言之,
为什么我的第一个线程 T2 return 在锁定释放后仍为 True?
如果我只执行 1 个线程,它会按预期工作。
当您将锁对象传递给构造函数时,它不会被复制,因此每个线程对象都可以访问完全相同的 锁。
这导致 self.lock.locked()
返回 True
因为锁 仍然被锁定 ,但这次它已被另一个 锁定线程.
实际情况是这样的:
>>> import threading
>>> lck = threading.Lock() # the 'original'
>>> lck2 = lck # lck2 isn't a copy of lck!
>>> lck.acquire()
True
>>> lck2.locked() # lck2 has been acquired as well as lck
True
>>> lck2.release()
>>> lck.locked() # lck has been released as well as lck2
False
同样,发生这种情况是因为锁对象没有(也不能)被复制,无论变量分配给某个锁,它都会指向原始对象。
我正在尝试使用 python 线程编写我的第一个代码。
请看代码 当我使用 release() 为线程释放锁时出现问题,因为它说锁仍然可用 [locked() returns True even after release()]
import threading
import time
class thread (threading.Thread):
def __init__(self,t,d,arr,lock):
threading.Thread.__init__(self)
self.name=t
self.delay=d
self.array=arr;
self.lock=lock
def run(self):
self.fun1()
def fun1(self):
print "the thread %s want to take a lock now" %self.name
self.lock.acquire()
print "lock status just after lock acquire foe",self.name,self.lock.locked()
time.sleep(self.delay)
print "release lock for %s" %self.name
self.lock.release()
time.sleep(2)
print "lock status after lock release is",self.name,self.lock.locked()
lck=threading.Lock()
obj1=thread('T1',5,[1,2,3],lck)
obj2=thread('T2',10,[4,5,6],lck)
obj1.start()
obj2.start()
输出
=====
the thread T1 want to take a lock now
the thread T2 want to take a lock now
lock status just after lock acquire foe T2 True
release lock for T2
lock status just after lock acquire foe T1 True
lock status after lock release is T2 True
release lock for T1
lock status after lock release is T1 False
我的问题是:
线程 T2 首先获得锁并执行它的 space。 我们可以使用 .locked() 看到 T2 的锁定状态是“
之后的锁定状态”lock acquire foe T2 True”。所以 T2 现在得到了锁。 一旦 T2 使用 release() 释放了锁,那么 T1 也按预期获得了锁
但是在使用 release() 释放 T2 的锁后,locked() 显示为 True。这意味着 T2 仍然可以锁定?这种情况下T1是怎么获取到锁的?。一旦 T1 执行并在 release() 之后,我可以看到 .locked() returns False。意味着对于 T1 locked() 一旦释放锁就按预期工作,立即 return False
简而言之, 为什么我的第一个线程 T2 return 在锁定释放后仍为 True? 如果我只执行 1 个线程,它会按预期工作。
当您将锁对象传递给构造函数时,它不会被复制,因此每个线程对象都可以访问完全相同的 锁。
这导致 self.lock.locked()
返回 True
因为锁 仍然被锁定 ,但这次它已被另一个 锁定线程.
实际情况是这样的:
>>> import threading
>>> lck = threading.Lock() # the 'original'
>>> lck2 = lck # lck2 isn't a copy of lck!
>>> lck.acquire()
True
>>> lck2.locked() # lck2 has been acquired as well as lck
True
>>> lck2.release()
>>> lck.locked() # lck has been released as well as lck2
False
同样,发生这种情况是因为锁对象没有(也不能)被复制,无论变量分配给某个锁,它都会指向原始对象。