我在 Python 中做了一个读写器(有作者偏见,例如写时不阅读)锁,由于其中一个作者没有释放锁,即使它打印了消息并且应该释放,它也不能正常工作之后的锁。
这是我的代码:
from threading import Thread
from threading import Lock
import time
import datetime
resource = 'hello world'
lock = Lock()
counter = 0
Lcounter = 0
class WriterA(Thread):
def run(self):
while True:
global lock
global resource
global counter
global Lcounter
time.sleep(5)
if Lcounter == 1:
if counter == -1:
print ("WriterA is trying to acquire the lock")
lock.acquire()
print ("WriterA acquired the lock")
time.sleep(5)
now = datetime.datetime.now()
temp = "%d/%m/%Y %H:%M:%S"
now_string = now.strftime(temp)
resource = now_string
print ("WriterA releasing the lock")
lock.release()
counter = 0
class WriterB(Thread):
def run(self):
while True:
global lock
global resource
global counter
global Lcounter
time.sleep(5)
if Lcounter == 2:
if counter == -1:
print ("WriterB is trying to acquire the lock")
lock.acquire()
print ("WriterB acquired the lock")
time.sleep(5)
print resource
resource = (resource [::-1])
print ("WriterB is releasing the lock")
lock.release()
counter = 0
class Reader(Thread):
def run(self):
global resource
global counter
global Lcounter
if counter >= 0:
time.sleep(5)
print ("Reader is trying to acquire the resource")
print ("Reader acquired the resource")
time.sleep(5)
temp = resource
print ("The message in %s is %s " % (self.name, temp))
Lcounter += 1
counter = -1
Reader().start()
time.sleep(5)
WriterA().start()
time.sleep(5)
Reader().start()
time.sleep(5)
WriterB().start()
time.sleep(5)
Reader().start()
输出:
Reader is trying to acquire the resource
Reader acquired the resource
The message in Thread-6 is hello world
WriterA is trying to acquire the lock
WriterA acquired the lock
WriterA releasing the lock
Reader is trying to acquire the resource
Reader acquired the resource
The message in Thread-10 is 06/05/2015 14:55:14
WriterB is trying to acquire the lock
WriterB acquired the lock
06/05/2015 14:55:14
WriterB is releasing the lock
这是我使用@User hang_threads 得到的输出:
Reader is trying to acquire the resource
Reader acquired the resource
The message in Thread-7 is hello world
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 267, in run
self.OnRun()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 325, in OnRun
r = self.sock.recv(1024)
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 267, in run
self.OnRun()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd.py", line 227, in OnRun
self._py_db_command_thread_event.wait(0.5)
File "C:\Python27\lib\threading.py", line 621, in wait
self.__cond.wait(timeout)
File "C:\Python27\lib\threading.py", line 359, in wait
_sleep(delay)
WriterA is trying to acquire the lock
WriterA acquired the lock
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 267, in run
self.OnRun()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 400, in OnRun
cmd = self.cmdQueue.get(1, 0.1)
File "C:\Python27\lib\Queue.py", line 177, in get
self.not_empty.wait(remaining)
File "C:\Python27\lib\threading.py", line 359, in wait
_sleep(delay)
WriterA releasing the lock
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 267, in run
self.OnRun()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 325, in OnRun
r = self.sock.recv(1024)
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 267, in run
self.OnRun()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd.py", line 227, in OnRun
self._py_db_command_thread_event.wait(0.5)
File "C:\Python27\lib\threading.py", line 621, in wait
self.__cond.wait(timeout)
File "C:\Python27\lib\threading.py", line 359, in wait
_sleep(delay)
Reader is trying to acquire the resource
Reader acquired the resource
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:/Users/Abdulkadir/PycharmProjects/labb5/labb5_final.py", line 44, in run
time.sleep(5)
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 267, in run
self.OnRun()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 400, in OnRun
cmd = self.cmdQueue.get(1, 0.1)
File "C:\Python27\lib\Queue.py", line 177, in get
self.not_empty.wait(remaining)
File "C:\Python27\lib\threading.py", line 359, in wait
_sleep(delay)
The message in Thread-11 is 06/05/2015 15:18:58
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 1107, in _exitfunc
t.join()
File "C:\Python27\lib\threading.py", line 949, in join
self.__block.wait()
File "C:\Python27\lib\threading.py", line 340, in wait
waiter.acquire()
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:/Users/Abdulkadir/PycharmProjects/labb5/labb5_final.py", line 20, in run
time.sleep(5)
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 267, in run
self.OnRun()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 325, in OnRun
r = self.sock.recv(1024)
WriterB is trying to acquire the lock
WriterB acquired the lock
06/05/2015 15:18:58
WriterB is releasing the lock
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 1107, in _exitfunc
t.join()
File "C:\Python27\lib\threading.py", line 949, in join
self.__block.wait()
File "C:\Python27\lib\threading.py", line 340, in wait
waiter.acquire()
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:/Users/Abdulkadir/PycharmProjects/labb5/labb5_final.py", line 20, in run
time.sleep(5)
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 267, in run
self.OnRun()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 325, in OnRun
r = self.sock.recv(1024)
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 267, in run
self.OnRun()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd.py", line 227, in OnRun
self._py_db_command_thread_event.wait(0.5)
File "C:\Python27\lib\threading.py", line 621, in wait
self.__cond.wait(timeout)
File "C:\Python27\lib\threading.py", line 359, in wait
_sleep(delay)
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 267, in run
self.OnRun()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 400, in OnRun
cmd = self.cmdQueue.get(1, 0.1)
File "C:\Python27\lib\Queue.py", line 177, in get
self.not_empty.wait(remaining)
File "C:\Python27\lib\threading.py", line 359, in wait
_sleep(delay)
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:/Users/Abdulkadir/PycharmProjects/labb5/labb5_final.py", line 44, in run
time.sleep(5)
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 1107, in _exitfunc
t.join()
File "C:\Python27\lib\threading.py", line 949, in join
self.__block.wait()
File "C:\Python27\lib\threading.py", line 340, in wait
waiter.acquire()
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:/Users/Abdulkadir/PycharmProjects/labb5/labb5_final.py", line 20, in run
time.sleep(5)
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 267, in run
self.OnRun()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 325, in OnRun
r = self.sock.recv(1024)
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 267, in run
self.OnRun()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd.py", line 227, in OnRun
self._py_db_command_thread_event.wait(0.5)
File "C:\Python27\lib\threading.py", line 621, in wait
self.__cond.wait(timeout)
File "C:\Python27\lib\threading.py", line 359, in wait
_sleep(delay)
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:/Users/Abdulkadir/PycharmProjects/labb5/labb5_final.py", line 44, in run
time.sleep(5)
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 1107, in _exitfunc
t.join()
File "C:\Python27\lib\threading.py", line 949, in join
self.__block.wait()
File "C:\Python27\lib\threading.py", line 340, in wait
waiter.acquire()
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:/Users/Abdulkadir/PycharmProjects/labb5/labb5_final.py", line 20, in run
time.sleep(5)
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 267, in run
self.OnRun()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 325, in OnRun
r = self.sock.recv(1024)
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 267, in run
self.OnRun()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd.py", line 227, in OnRun
self._py_db_command_thread_event.wait(0.5)
File "C:\Python27\lib\threading.py", line 621, in wait
self.__cond.wait(timeout)
File "C:\Python27\lib\threading.py", line 359, in wait
_sleep(delay)
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:/Users/Abdulkadir/PycharmProjects/labb5/labb5_final.py", line 44, in run
time.sleep(5)
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 1107, in _exitfunc
t.join()
File "C:\Python27\lib\threading.py", line 949, in join
self.__block.wait()
File "C:\Python27\lib\threading.py", line 340, in wait
waiter.acquire()
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:/Users/Abdulkadir/PycharmProjects/labb5/labb5_final.py", line 20, in run
time.sleep(5)
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 267, in run
self.OnRun()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 325, in OnRun
r = self.sock.recv(1024)
-------------------- Thread 11612 --------------------
File "C:\Python27\lib\threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd_comm.py", line 267, in run
self.OnRun()
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\helpers\pydev\pydevd.py", line 227, in OnRun
self._py_db_command_thread_event.wait(0.5)
File "C:\Python27\lib\threading.py", line 621, in wait
self.__cond.wait(timeout)
File "C:\Python27\lib\threading.py", line 359, in wait
_sleep(delay)