Jzl5325
Jzl5325

Reputation: 3974

Python multiprocessing keyword arguments

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?

Upvotes: 15

Views: 14192

Answers (1)

Andrew Cox
Andrew Cox

Reputation: 10988

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

Upvotes: 22

Related Questions