Python 多处理关键字参数 [英] Python multiprocessing keyword arguments

查看:35
本文介绍了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)]

这篇关于Python 多处理关键字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆