问题
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