问题描述
我遇到了多处理问题.代码包含在下面.代码可以按预期执行,但是当取消注释self.queue = multiprocessing.Queue()
时,该程序会立即退出,并且似乎无法成功启动子进程.
I encounter a multiprocessing problem. The code is included below. The code can execute as expected, but when uncommenting self.queue = multiprocessing.Queue()
, this program will exit immediately and it seems that the subprocess can't be started successfully.
我不知道发生了什么.有人可以帮我吗?非常感谢!
I don't know what happened. Could someone help me out? Many Thanks!
import multiprocessing
import time
class Test:
def __init__(self):
self.pool = multiprocessing.Pool(1)
#self.queue = multiprocessing.Queue()
def subprocess(self):
for i in range(10):
print("Running")
time.sleep(1)
print("Subprocess Completed")
def start(self):
self.pool.apply_async(func=self.subprocess)
print("Subprocess has been started")
self.pool.close()
self.pool.join()
def __getstate__(self):
self_dict = self.__dict__.copy()
del self_dict['pool']
return self_dict
def __setstate__(self, state):
self.__dict__.update(state)
if __name__ == '__main__':
test = Test()
test.start()
推荐答案
我可以重现你的问题,也没有 Traceback
提出.
这应该会引发以下错误,不知道为什么:
I can reproduce your Issue and also no Traceback
raised up.
This should raise the following error, don't know why not:
运行时错误:队列对象只能通过继承在进程之间共享
将您的代码行替换为:
m = multiprocessing.Manager()
self.queue = m.Queue()
为什么会发生这种情况?multiprocessing.Queue
用于 one Process
,您使用的是 multiprocessing.Pool
,它使用多个 Process
,你得用multiprocessing.Manager().Queue()
.
Why does this happens?multiprocessing.Queue
is for use with one Process
, you are using multiprocessing.Pool
, which uses multiple Process
, you have to usemultiprocessing.Manager().Queue()
.
使用 Python 测试:3.4.2
这篇关于Python 3.5 多处理池和队列不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!