Python Keyword Arguments for Multiprocessing

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) 

What print, as expected:

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

I am trying to pass the same style keyword arguments for a multiprocessing task, but using ** 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 multiprocessor .Process? If so, how? If not, why?

+8
python arguments multiprocessing
source share
1 answer

The dictionary that you use as the args keywords should be passed as kwargs 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)] 
+15
source share

All Articles