将管道/连接作为上下文arg传递给多处理Pool.apply

编程入门 行业动态 更新时间:2024-10-26 00:28:35
本文介绍了将管道/连接作为上下文arg传递给多处理Pool.apply_async()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用管道与池中的流程实例进行通信,但是出现错误:

I want to use pipes to talk to the process instances in my pool, but I'm getting an error:

让__p成为Pool()的实例:

Let __p be an instance of Pool():

(master_pipe, worker_pipe) = Pipe() self.__p.apply_async(_worker_task, (handler_info, context_info, worker_pipe))

执行此操作时,[显然,对于每个实例]都会出现以下错误:

When I execute this, I get the following error [for every instance, obviously]:

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实例.如果我将其设置为无",则工作人员会毫无错误地进行分叉.

The error is specifically referring to the Connection instance that I'm trying to pass. If I make it "None", the workers fork without error.

我不明白这一点,因为正如该文档通过示例强调的那样,我可以轻松地将相同的参数传递给Process()并使其完美运行:

I don't understand this since, as the document emphasizes through example, I can easily pass the same argument to a Process(), and have it work perfectly:

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

推荐答案

这是一个 bug ,其中包含已在Python 3中修复.

This is a bug which has been fixed in Python 3.

最简单的解决方案是按照其他答案中的建议,通过队列的初始化程序传递队列.

Easiest solution is to pass the queue through the Pool's initializer as suggested in the other answer.

更多推荐

将管道/连接作为上下文arg传递给多处理Pool.apply

本文发布于:2023-08-04 23:09:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1300402.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   多处   管道   arg   Pool

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!