Python 如何启动两个线程并在运行前锁定它们,并且仅在解锁时执行?
我有以下示例代码来解释我的问题:Python 如何启动两个线程并在运行前锁定它们,并且仅在解锁时执行?,python,multithreading,python-2.7,locking,Python,Multithreading,Python 2.7,Locking,我有以下示例代码来解释我的问题: import threading import time class thread1(threading.Thread): def __init__(self, lock): threading.Thread.__init__(self) self.daemon = True self.start() self.lock = lock def run(self)
import threading
import time
class thread1(threading.Thread):
def __init__(self, lock):
threading.Thread.__init__(self)
self.daemon = True
self.start()
self.lock = lock
def run(self):
while True:
self.lock.acquire(True)
print ('write done by t1')
self.lock.release()
class thread2(threading.Thread):
def __init__(self, lock):
threading.Thread.__init__(self)
self.daemon = True
self.start()
self.lock = lock
def run(self):
while True:
self.lock.acquire(True)
print ('write done by t2')
self.lock.release()
if __name__ == '__main__':
lock = threading.Lock()
t1 = thread1(lock)
t2 = thread2(lock)
lock.acquire(True)
counter = 0
while True:
print("main...")
counter = counter + 1
if(counter==5 or counter==10):
lock.release() # Here I want to unlock both threads to run just one time and then wait until I release again
time.sleep(1)
t1.join()
t2.join()
我遇到的一些问题如下:
我希望在程序开始时启动两个线程(thread1和thread2),但它们应该等到main()
计数器达到5或10
当main()
我希望代码有以下输出(每行运行1秒):
相反,我有一种不同的行为,比如从:
write done by t1
write done by t1
write done by t1
write done by t1
(等)
5秒后
write done by t2
很多时候
有人能帮我解释什么是错误的,我该如何改进
在thread1和thread2的_init__()中,在分配self.lock之前调用start()
t1和t2是在主线程获取锁之前创建的。这使得这两个线程在主线程锁定它们之前开始打印。这就是您的代码打印“write done by x”的前几行的原因
计数器达到5后,主线程释放锁,但它不会再次锁定它。这使得t1和t2继续运行
它永远不会放弃,除非你杀了它
我建议你用锁代替
下面是一个基于您的代码的示例
import threading
import time
class Thread1(threading.Thread):
def __init__(self, condition_obj):
super().__init__()
self.daemon = True
self.condition_obj = condition_obj
self.start()
def run(self):
with self.condition_obj:
while True:
self.condition_obj.wait()
print('write done by t1')
class Thread2(threading.Thread):
def __init__(self, condition_obj):
super().__init__()
self.daemon = True
self.condition_obj = condition_obj
self.start()
def run(self):
with self.condition_obj:
while True:
self.condition_obj.wait()
print('write done by t2')
if __name__ == '__main__':
condition = threading.Condition()
t1 = Thread1(condition)
t2 = Thread2(condition)
counter = 0
while True:
print("main...")
counter += 1
if counter == 5 or counter == 10:
with condition:
condition.notify_all()
time.sleep(1)
t1.join()
t2.join()
import threading
import time
class Thread1(threading.Thread):
def __init__(self, condition_obj):
super().__init__()
self.daemon = True
self.condition_obj = condition_obj
self.start()
def run(self):
with self.condition_obj:
while True:
self.condition_obj.wait()
print('write done by t1')
class Thread2(threading.Thread):
def __init__(self, condition_obj):
super().__init__()
self.daemon = True
self.condition_obj = condition_obj
self.start()
def run(self):
with self.condition_obj:
while True:
self.condition_obj.wait()
print('write done by t2')
if __name__ == '__main__':
condition = threading.Condition()
t1 = Thread1(condition)
t2 = Thread2(condition)
counter = 0
while True:
print("main...")
counter += 1
if counter == 5 or counter == 10:
with condition:
condition.notify_all()
time.sleep(1)
t1.join()
t2.join()