小编Yin*_*_90的帖子

python多处理队列中的死锁

我正在使用多处理库中的队列在进程之间共享数据。

我有 2 个队列,都限制为 10 个对象,第一个队列有一个进程将对象“放入”其中,许多进程从中“获取”。

第二个队列有许多进程将对象“放入”其中,只有一个进程从中“获取”。

系统完美地工作了一段时间,然后开始表现奇怪:只有将对象“放入”第一个队列的进程继续工作,而从第一个队列读取的进程显然不再行为/工作(即使进程是活)。似乎这里有一个死锁,但我不确定,这是我的代码:

更新

import multiprocessing
import logging
from multiprocessing import Process

logger = logging.get_logger(__name__)

# Processes 2, 3 ,4:

class Processes_234(Process):
    def __init__(self, message_queue_1, message_queue_2):
        Process.__init__(self)
        self.message_queue_1 = message_queue_1
        self.message_queue_2 = message_queue_2

    def run(self):
        while True:
            try:
                # get from queue
                el1, el2, el3 = self.message_queue_1.get()
                logger.debug('Processes234: get from queue')
            except Exception as exp:
                logger.debug("message_queue_1: queue empty, Exception message: " + str(exp))

            # do some stuff with el1, el2, el3...

            try:
                # put into …
Run Code Online (Sandbox Code Playgroud)

python deadlock message-queue multiprocessing python-3.x

5
推荐指数
1
解决办法
735
查看次数