鉴于此bug(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

没有人知道一个解决方法为在一个队列通过一个连接对象?

感谢您。

有帮助吗?

解决方案

(我认为是)一个更好的方法,经过一番发现这个帖子之前玩弄(我有同样的问题,想通过管道传递的管道。):

>>> 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 bug报告让其他人知道的解决办法的。

其他提示

下面是大概我所做的:

# 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))

希望帮助别人。它工作正常的我。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top