Python 多处理关键字参数

Python 多处理关键字参数

问题描述:

这是在函数调用中使用关键字参数的简单示例.没什么特别的.

Here is a simple example of using keyword arguments in a function call. Nothing special.

def foo(arg1,arg2, **args):
    print arg1, arg2
    print (args)
    print args['x']

args ={'x':2, 'y':3}
foo(1,2,**args)

按预期打印:

1 2
{'y': 3, 'x': 2}
2

我试图将相同样式的关键字参数传递给多处理任务,但是在 args 列表中使用 **, 是一个语法错误.我知道我的函数stretch() 将采用两个位置参数和n 个关键字参数.

I am trying to pass the same style keyword arguments to a multiprocessing task, but the use of **, in the args list is a syntax error. I know that my function, stretch() will take two positional arguments and n keyword arguments.

pool = [multiprocessing.Process(target=stretch, args= (shared_arr,slice(i, i+step),**args)) for i in range (0, y, step)]

是否可以将关键字参数传递给 multiprocessing.Process?如果是这样,如何?如果没有,为什么?

Is it possible to pass keyword arguments to a multiprocessing.Process? If so, how? If not, why?

您用作关键字参数的字典应作为 kwargs 参数传入 Process对象.

The dictionary you are using as keyword args should be passed in as the kwargs parameter to the Process object.

pool = [multiprocessing.Process(target=stretch, args= (shared_arr,slice(i, i+step)),kwargs=args) for i in range (0, y, step)]