我有一个定义队列并在后台启动大量工作程序的类:
class WorkerMaster:
def __init__(self):
self.workers = []
def listen(self):
queue = Queue()
self.init_workers(queue)
while True:
data = fetchData() # Fetches data from somewhre
queue.put(data)
def init_workers(self, queue):
for i in range(5):
worker = Worker(queue)
self._workers.append(worker)
worker.daemon = True
worker.start()
如您所见,师父基本上是制作人。他从某处获取数据并将其添加到队列中。
这是从Process继承的辅助类的run方法:
def run(self):
try:
super().run()
while True:
data = self.queue.get()
self.process(data)
这里的问题是get()调用正在阻塞,并且当我尝试销毁主进程时,队列首先被销毁,而在子进程中,我遇到了诸如此类的错误,因为该队列不存在:
File "C:\python\python\3.7.1\lib\multiprocessing\queues.py", line 94, in get
res = self._recv_bytes()
File "C:\python\python\3.7.1\lib\multiprocessing\connection.py", line 216, in recv_bytes
buf = self._recv_bytes(maxlength)
File "C:\python\python\3.7.1\lib\multiprocessing\connection.py", line 306, in _recv_bytes
[ov.event], False, INFINITE)
这将使主进程停滞,不允许其正确退出。我该如何解决该问题?