gpt4 book ai didi

Python multiprocessing 和 multiprocessing.Queue

转载 作者:行者123 更新时间:2023-11-28 22:02:46 28 4
gpt4 key购买 nike

我正在尝试使用多处理和队列实现生产者-消费者场景;主进程是生产者,两个子进程使用队列中的数据。这在没有任何异常 发生的情况下有效,但问题是我希望能够在工作人员死亡时重新启动他们(kill -9 workerpid)。但是,当我杀死一个或两个 worker 时,即使主进程一直在队列中填充数据,他们也会开始说“队列为空”。

我在这里错过了什么? (在 Ubuntu 12.04 上使用 Python 2.7.3)

import sys
import time

import multiprocessing
from Queue import Empty

workers = []
fqueue = multiprocessing.Queue()

class Worker(multiprocessing.Process):
def run(self):
queue = self._args[0]
print "{0} starting up, queue at: {1}".format(self.pid, queue)
while True:
try:
obj = queue.get(block=True, timeout=1)
print "{0}: got from queue: {1}".format(self.pid, obj)
except Empty:
print "{0}: queue was empty".format(self.pid)
continue
except IOError, e:
print "{0}: got IOError on queue: {1}".format(self.pid, e)
return

if __name__ == "__main__":
print "zipper starting up (2 workers)"
for _ in range(0, 2):
p = Worker(args=(fqueue,))
workers.append(p)
p.start()

cnt = 0
while True:
for i in range(0, len(workers)):
p = workers[i]
if not p.is_alive():
print "main: worker {0} is not alive".format(p.pid)
p = Worker(args=(fqueue,))
print "main: restarted worker: {0}".format(p)
p.start()
workers[i] = p
print "main: tick"
cnt += 1
fqueue.put(cnt)
time.sleep(2)

最佳答案

你见过warning in the documentation吗? ?:

Warning

If a process is killed using Process.terminate() or os.kill() while it is trying to use a Queue, then the data in the queue is likely to become corrupted. This may cause any other process to get an exception when it tries to use the queue later on.

因此,终止使用队列的进程会使整个队列无法使用。

关于Python multiprocessing 和 multiprocessing.Queue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10794903/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com