11

鉴于此错误(Python 问题 4892)会导致以下错误:

>>> import multiprocessing
>>> multiprocessing.allow_connection_pickling()
>>> q = multiprocessing.Queue()
>>> p = multiprocessing.Pipe()
>>> q.put(p)
>>> q.get()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File     "/.../python2.6/multiprocessing/queues.py", line 91, in get
    res = self._recv()
TypeError: Required argument 'handle' (pos 1) not found

有谁知道在队列上传递 Connection 对象的解决方法?

谢谢你。

4

2 回答 2

8

这大致是我所做的:

# Producer
from multiprocessing.reduction import reduce_connection
from multiprocessing import Pipe

   # Producer and Consumer share the Queue we call queue
def handle(queue):
   reader, writer = Pipe()
   pickled_writer = pickle.dumps(reduce_connection(writer))
   queue.put(pickled_writer)

# Consumer
from multiprocessing.reduction import rebuild_connection

def wait_for_request():
    pickled_write = queue.get(block=True) # block=True isn't necessary, of course
    upw = pickle.loads(pickled_writer) # unpickled writer
    writer = upw[0](upw[1][0],upw[1][1],upw[1][2])

最后一行是神秘的,来自以下内容:

>>> upw
(<function rebuild_connection at 0x1005df140>,
(('/var/folders/.../pymp-VhT3wX/listener-FKMB0W',
17, False), True, True))

希望对其他人有所帮助。这对我来说可以。

于 2009-09-18T19:20:09.097 回答
8

(我相信是)一种更好的方法,经过一番玩耍(我遇到了同样的问题。想通过管道穿过管道。)在发现这篇文章之前:

>>> from multiprocessing import Pipe, reduction
>>> i, o = Pipe()
>>> reduced = reduction.reduce_connection(i)
>>> newi = reduced[0](*reduced[1])
>>> newi.send("hi")
>>> o.recv()
'hi'

我不完全确定为什么要以这种方式构建(有人需要深入了解多处理的减少部分到底是什么),但它确实有效,并且不需要导入泡菜。除此之外,它的功能与上述非常接近,但更简单。我还把它扔进了 python 错误报告中,以便其他人知道解决方法。

于 2011-01-18T02:24:23.047 回答