Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python multiprocess.apply\u async如何包装*args和**kwargs?_Python_Multiprocessing_Keyword Argument - Fatal编程技术网

Python multiprocess.apply\u async如何包装*args和**kwargs?

Python multiprocess.apply\u async如何包装*args和**kwargs?,python,multiprocessing,keyword-argument,Python,Multiprocessing,Keyword Argument,我试图同时接受*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,

我试图同时接受
*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)

如何正确包装
多进程。应用异步

您不必显式使用
*
**
。只需传递元组和dict,然后让
apply\u 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
one参数,调用
func
时解包,
kwargs
one参数,并以相同的方式处理。还要注意,在
**kwargs
之后不可能有其他参数:

>>> def test(**kwargs, something=True): pass

  File "<stdin>", line 1
    def test(**kwargs, something=True): pass
                     ^
SyntaxError: invalid syntax
def测试(**kwargs,something=True):通过 文件“”,第1行 def测试(**kwargs,something=True):通过 ^ SyntaxError:无效语法
>>> def test(**kwargs, something=True): pass

  File "<stdin>", line 1
    def test(**kwargs, something=True): pass
                     ^
SyntaxError: invalid syntax