问题描述
这是在函数调用中使用关键字参数的简单示例.没什么特别的.
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?
推荐答案
用作关键字args的字典应作为 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)]
这篇关于Python多处理关键字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!