我想使用管道与池中的流程实例通信,但出现错误:
让 __p 成为 Pool() 的一个实例:
(master_pipe, worker_pipe) = Pipe()
self.__p.apply_async(_worker_task,
(handler_info,
context_info,
worker_pipe))
当我执行此操作时,我收到以下错误[显然对于每个实例]:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/queues.py", line 376, in get
task = get()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/queues.py", line 376, in get
TypeError: Required argument 'handle' (pos 1) not found
self.run()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/process.py", line 114, in run
return recv()
return recv()
self._target(*self._args, **self._kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 102, in worker
TypeError: Required argument 'handle' (pos 1) not found
TypeError: Required argument 'handle' (pos 1) not found
task = get()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/queues.py", line 376, in get
return recv()
TypeError: Required argument 'handle' (pos 1) not found
该错误具体指的是我尝试传递的 Connection 实例。如果我将其设置为“无”,则工作人员 fork 时不会出现错误。
我不明白这一点,因为正如文档通过示例强调的那样,我可以轻松地将相同的参数传递给 Process(),并使其完美工作:
from multiprocessing import Pipe, Process
def call_me(p):
print("Here: %s" % (p))
(master, worker) = Pipe()
p = Process(target=call_me, args=(worker,))
p.start()
Here: <read-write Connection, handle 6>
p.join()
最佳答案
看起来像这个讨论中提到的错误( http://bugs.python.org/issue4892 ): Python 2.6 send connection object over Queue / Pipe / etc
池最初使用管道来 fork 子进程,以便与子进程通信任务/结果。它是在通过现有管道与 Pipe 对象进行通信时发生爆炸的,而不是在 fork 上。 (失败是当子进程尝试对队列抽象执行 get() 时)。
看起来问题的出现是由于 Pipe 对象是如何腌制/取消腌制以进行通信的。
在您注意到的第二种情况下,管道被传递到流程实例,然后 fork - 因此行为有所不同。
我无法想象在纯任务分配之外与池进程主动通信是多处理池的预期用例。就状态/协议(protocol)而言,这意味着您希望对流程有更多控制。这将需要比一般 Pool 对象所能知道的更多的上下文。
关于python - 将管道/连接作为上下文参数传递给多处理 Pool.apply_async(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20080559/