質問

次のエラーが発生するこのバグ(Python Issue 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オブジェクトを渡すための回避策を知っている人はいますか?

ありがとう。

役に立ちましたか?

解決

(私が信じているもの)この投稿を発見する前に、いくつかの遊びの後(私は同じ問題を抱えていた。パイプをパイプに通したかった。)、より良い方法:

>>> 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バグレポートに入れて、他の人が回避策を知っているようにしました。

他のヒント

おおよそ次のようになりました:

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

and

# 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