我正在尝试multiprocess.apply_async
同时接纳*args
和**kwargs
。文档表明调用序列可以实现这一点:
apply_async(func[, args[, kwds[, callback]]])
但我无法弄清楚如何使调用语法正确。用最小的例子:
from multiprocessing import Pool
def f(x, *args, **kwargs):
print x, args, kwargs
args, kw = (), {}
print "# Normal call"
f(0, *args, **kw)
print "# Multicall"
P = Pool()
sol = [P.apply_async(f, (x,), *args, **kw) for x in range(2)]
P.close()
P.join()
for s in sol: s.get()
这按预期工作,提供输出
# Normal call
0 () {}
# Multicall
0 () {}
1 () {}
当args不是空元组时,例如args = (1,2,3)
,单个调用有效,但多处理解决方案给出:
# Normal call
0 (1, 2, 3) {}
# Multicall
Traceback (most recent call last):
File "kw.py", line 16, in <module>
sol = [P.apply_async(f, (x,), *args, **kw) for x in range(2)]
TypeError: apply_async() takes at most 5 arguments (6 given)
我得到了kwargs参数,例如kw = {'cat':'dog'}
# Normal call
0 () {'cat': 'dog'}
# Multicall
Traceback (most recent call last):
File "kw.py", line 15, in <module>
sol = [P.apply_async(f, (x,), *args, **kw) for x in range(2)]
TypeError: apply_async() got an unexpected keyword argument 'cat'
如何正确包裹multiprocess.apply_async
?
答案 0 :(得分:14)
您不必明确使用*
和**
。只需传递元组和字典,然后让apply_async
解压缩它们:
from multiprocessing import Pool
def f(x, *args, **kwargs):
print x, args, kwargs
args, kw = (1,2,3), {'cat': 'dog'}
print "# Normal call"
f(0, *args, **kw)
print "# Multicall"
P = Pool()
sol = [P.apply_async(f, (x,) + args, kw) for x in range(2)]
P.close()
P.join()
for s in sol: s.get()
输出:
# Normal call
0 (1, 2, 3) {'cat': 'dog'}
# Multicall
0 (1, 2, 3) {'cat': 'dog'}
1 (1, 2, 3) {'cat': 'dog'}
请记住,在python的文档中,如果函数接受*args
和**kwargs
,则其签名明确指出:
the_function(a,b,c,d, *args, **kwargs)
在你的情况下:
apply_async(func[, args[, kwds[, callback]]])
那里没有*
,因此args
是一个参数,在调用func
且kwargs
为时会解压缩一个参数并以相同的方式处理。另请注意,**kwargs
之后不可能有其他参数:
>>> def test(**kwargs, something=True): pass
File "<stdin>", line 1
def test(**kwargs, something=True): pass
^
SyntaxError: invalid syntax