multiprocessing.queue.get() 阻塞并且不返回

Dak*_*ron 5 queue multiprocessing python-2.7

我正在一个带有很少 RAM(只有 256 MB)的嵌入式 Linux 设备上构建一个网络服务器。该网络服务器应该能够使用 发出 shell 命令subprocess.check_output,但是由于每个check_output似乎都需要与父进程消耗的 RAM 一样多的可用 RAM,因此当 Python 仍然没有消耗太多内存时,我正在使用多处理在开始时创建第二个进程。然后,此进程使用 amultiprocessing.Queue从主进程接收命令,执行它们并使用另一个 返回输出multiprocessing.Queue。这曾经有效,但我似乎有某种竞争条件导致整个事情陷入困境。

这是我复制问题的最小测试用例:

# shwrapper.py
from multiprocessing import Process, Queue

def f(iq,oq):
    oq.put("Ready")
    while True:
        oq.put(iq.get()+" out")

def init():
    iq = Queue()
    oq = Queue()
    p = Process(target=f, args=(iq,oq,))
    p.start()
    print oq.get()
    iq.put("test")
    print(oq.get())
init()
Run Code Online (Sandbox Code Playgroud)

如果我尝试导入它,我会得到这个结果:

>>> import shwrapper
Ready
Run Code Online (Sandbox Code Playgroud)

在这里它卡住了。现在我发出一个KeyboardInterrupt:

^CProcess Process-1:
Traceback (most recent call last):
  File "/usr/lib/python27.zip/multiprocessing/process.py", line 258, in _bootstrap
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "shwrapper2.py", line 17, in <module>
    init()
  File "shwrapper2.py", line 15, in init
    print(oq.get())
  File "/usr/lib/python27.zip/multiprocessing/queues.py", line 117, in get
KeyboardInterrupt
    self.run()
  File "/usr/lib/python27.zip/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "shwrapper2.py", line 6, in f
    oq.put(iq.get()+" out")
  File "/usr/lib/python27.zip/multiprocessing/queues.py", line 117, in get
    res = self._recv()
KeyboardInterrupt
Run Code Online (Sandbox Code Playgroud)

如您所见,它卡在 f 中的 iq.get() 处。iq-Queue 报告始终为空,无论我放入什么。另一方面,oq-Queue 按预期工作。任何想法我可以在这里做什么?

我的系统是在 PowerPC 上的 Linux 2.6.29.6 上运行的 Python 2.7.3。

如果我在 Windows 计算机上的 Python 2.7.10 上运行它,一切都会顺利。

我知道 Python 2.7.3 和 Linux 2.6.29 很古老,但是制造商没有更新的版本,而且系统有点锁定。

Dak*_*ron 5

我终于通过使用multiprocessing.queues.SimpleQueue而不是multiprocessing.Queue. 根据文档,在这方面应该没有区别,但在源代码中SimpleQueue使用Locks 使其适用于多个进程。