虽然我认为@TesselatingHeckler提供了答案,但这个评论并不适合放在评论区...所以它是那个答案的扩展。
您无法对multiprocessing
Queue
和Pipe
对象进行pickle,实际上,一些pickled对象仅在load
时失败。
我已经构建了一个multiprocessing
分支,允许大多数对象被pickle。主要做的事情是用一个更强大的序列化程序(dill
)替换pickle
。该分支作为pathos
包的一部分(在github上)可用。我以前尝试过序列化Pipes
,它可以工作,它也可以在python的socket
和python的Queue
上工作。然而,显然它仍然不能在 Queue
上工作。
>>> from processing import Pipe
>>> p = Pipe()
>>>
>>> import dill
>>> dill.loads(dill.dumps(p))
(Connection(handle=12), Connection(handle=14))
>>>
>>> from socket import socket
>>> s = socket()
>>> from Queue import Queue as que
>>> w = que()
>>> dill.loads(dill.dumps(s))
<socket._socketobject object at 0x10dae18a0>
>>> dill.loads(dill.dumps(w))
<Queue.Queue instance at 0x10db49f38>
>>>
>>> from processing import Queue
>>> q = Queue()
>>> dill.loads(dill.dumps(q))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mmckerns/lib/python2.7/site-packages/dill-0.2.2.dev-py2.7.egg/dill/dill.py", line 180, in dumps
dump(obj, file, protocol, byref, file_mode, safeio)
File "/Users/mmckerns/lib/python2.7/site-packages/dill-0.2.2.dev-py2.7.egg/dill/dill.py", line 173, in dump
pik.dump(obj)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 224, in dump
self.save(obj)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 306, in save
rv = reduce(self.proto)
File "/Users/mmckerns/lib/python2.7/site-packages/processing/queue.py", line 62, in __getstate__
assertSpawning(self)
File "/Users/mmckerns/lib/python2.7/site-packages/processing/forking.py", line 24, in assertSpawning
'processes through inheritance' % type(self).__name__)
RuntimeError: Queue objects should only be shared between processes through inheritance
看起来,对于multiprocessing
的Queue
,__getstate__
方法被硬编码为会抛出一个错误。如果你将其嵌入到一个类中,可能直到类实例从pickle中重新构建时才触发。
尝试使用pickle
对multiprocessing
的Queue
进行pickle也会导致上述错误。