multiprocessing.Queue中的ctx参数 [英] ctx parameter in multiprocessing.Queue
问题描述
我正在尝试使用multiprocessing.Queue模块中的Queue. 实施( https://docs.python.org/3.4/library/multiprocessing.html#exchanging-objects-between-processes )使用
I´m trying to use a Queue from the multiprocessing.Queue module. The Implementation (https://docs.python.org/3.4/library/multiprocessing.html#exchanging-objects-between-processes) uses
q = Queue()
作为实例化的例子.如果我尝试这样做,会出现以下错误:
as an example for instantiation. If i try this, i get the following error:
TypeError: __init__() missing 1 required keyword-only argument: 'ctx'
谷歌搜索出现了这个问题:
Googling the problem brought up this:
http://bugs.python.org/issue21367
我如何知道此问题是否已解决?现在不可能使用multiprocessing.Queues吗? 如果没有,我如何获取所需的ctx对象(这是什么?)
How do i know if this is fixed? Is it impossible to use multiprocessing.Queues right now? If not, how do i get the needed ctx object (and what is it?)
推荐答案
听起来您不是直接从multiprocessing
导入Queue
.引入上下文后,从multiprocessing
顶级包中导入的大多数对象都变成了在内部获取上下文的函数,然后将其传递给基础类初始化程序,而不是成为类本身.例如,这是multiprocessing.Queue
现在的样子:
It sounds like you're not importing Queue
directly from multiprocessing
. When contexts were introduced, most of the objects you import from the multiprocessing
top-level package became functions which internally get a context, then pass that to an underlying class initializer, rather than being classes themselves. For example, here is what multiprocessing.Queue
is now:
def Queue(self, maxsize=0):
'''Returns a queue object'''
from .queues import Queue
return Queue(maxsize, ctx=self.get_context())
如果直接导入multiprocessing.queues.Queue
并尝试实例化它,则会得到您看到的错误.但是,如果直接从multiprocessing
导入它,它应该可以正常工作.
If you were to import multiprocessing.queues.Queue
directly and try to instantiate it, you'll get the error you're seeing. But it should work fine if you import it from multiprocessing
directly.
上下文对象告诉multiprocessing
哪个正在使用启动子流程的可用方法. multiprocessing.Queue
在内部使用multiprocessing.Lock
,它必须知道正确的上下文才能正常运行.
The context object tells multiprocessing
which of the available methods for starting sub-processes is in use. multiprocessing.Queue
uses a multiprocessing.Lock
internally, which has to know the correct context to function properly.
这篇关于multiprocessing.Queue中的ctx参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!