Python multiprocessing keyword arguments

拥有回忆 提交于 2021-01-21 06:43:32

问题


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)

Which prints, as expected:

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

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

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


回答1:


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


来源:https://stackoverflow.com/questions/11546858/python-multiprocessing-keyword-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!