Python 生产者/消费者:我应该为消费者编写condition.release()吗?
当我在研究一个来自的示例时,我看到如果我将Python 生产者/消费者:我应该为消费者编写condition.release()吗?,python,multithreading,Python,Multithreading,当我在研究一个来自的示例时,我看到如果我将self.condition.release()放入Consumer类中的注释中,程序仍会像以前一样执行。这部分必要吗?我应该写吗 提前谢谢 代码为: #!/usr/bin/env python3 # -*- coding: utf-8 -*- import time import threading class Producer(threading.Thread): def __init__(self, condition, variab
self.condition.release()
放入Consumer类中的注释中,程序仍会像以前一样执行。这部分必要吗?我应该写吗
提前谢谢
代码为:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
import threading
class Producer(threading.Thread):
def __init__(self, condition, variables):
threading.Thread.__init__(self)
self.condition = condition
self.variables = variables
def run(self):
count = 1
while count < 10:
self.condition.acquire()
print("condition was acquired by {}".format(self.name))
self.variables.append(count)
print("'{}' was appended to list by {}".format(count, self.name))
self.condition.notify()
print("condition was notified by {}".format(self.name))
self.condition.release()
print("condition was released by {}".format(self.name))
count += 1
time.sleep(0.5)
class Consumer(threading.Thread):
def __init__(self, condition, variables):
threading.Thread.__init__(self)
self.condition = condition
self.variables = variables
def run(self):
while True:
self.condition.acquire()
print("condition was acquired by {}".format(self.name))
while True:
if self.variables:
number = self.variables.pop()
print("'{}', was popped from list by {}".format(number, self.name))
break
print("condition is waited by {}".format(self.name))
self.condition.wait()
# The part that i talked about is the below.
# Should i put it out of the comment?
# self.condition.release()
# print("condition was released by {}".format(self.name))
__condition__ = threading.Condition()
__variables__ = []
t1 = Producer(condition=__condition__, variables=__variables__)
t2 = Consumer(condition=__condition__, variables=__variables__)
t1.start()
t2.start()
#/usr/bin/env蟒蛇3
#-*-编码:utf-8-*-
导入时间
导入线程
类生成器(threading.Thread):
定义初始化(自身、条件、变量):
threading.Thread.\uuuuu init\uuuuuu(自)
自我状态
self.variables=变量
def运行(自):
计数=1
当计数小于10时:
self.condition.acquire()
打印(“条件是由{}获取的”。格式(self.name))
self.variables.append(计数)
打印(“{}”被{}追加到列表中。格式(count,self.name))
self.condition.notify()
打印(“条件由{}通知”。格式(self.name))
self.condition.release()
打印(“条件由{}发布”。格式(self.name))
计数+=1
睡眠时间(0.5)
类使用者(threading.Thread):
定义初始化(自身、条件、变量):
threading.Thread.\uuuuu init\uuuuuu(自)
自我状态
self.variables=变量
def运行(自):
尽管如此:
self.condition.acquire()
打印(“条件是由{}获取的”。格式(self.name))
尽管如此:
如果自变量为:
number=self.variables.pop()
打印(“{}”,由{}从列表中弹出。格式(数字,self.name))
打破
打印(“条件由{}等待”。格式(self.name))
self.condition.wait()
#我谈到的部分如下。
#我应该把它从评论中删掉吗?
#self.condition.release()
#打印(“条件由{}发布”。格式(self.name))
__条件=线程。条件()
__变量
t1=生产者(条件=\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
t2=消费者(条件=\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
t1.start()
t2.start()
是的,那部分是必需的。否则,当使用者线程在
循环期间中断时,它将继续保持锁,可能导致死锁
您当前的代码没有中断的原因是您只有一个消费者,而消费者从未中断循环。感谢您的澄清。