多处理池的方法需要 python 中的多个参数
Multiprocessing pool with a method that requires more than one argument in python
我是多处理的新手,python,从文档中,
https://docs.python.org/3/library/multiprocessing.html
我能够 运行 下面的代码。
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
with Pool(5) as p:
print(p.map(f, [1, 2, 3]))
但是如果 f(x)
更改为 f(x,y,z)
这样
def f(x, y, z):
return x*y*z
从 p.map
方法向 f(x, y, z)
传递 3 个参数的语法是什么?
使用p.starmap()
,正是针对这种情况。
也许您正在寻找 starmap
?
starmap(func, iterable[, chunksize])
Like map() except that the elements of the iterable are expected to be iterables that are unpacked as arguments.
Hence an iterable of [(1,2), (3, 4)]
results in [func(1,2), func(3,4)]
.
New in version 3.3.
我是多处理的新手,python,从文档中, https://docs.python.org/3/library/multiprocessing.html
我能够 运行 下面的代码。
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
with Pool(5) as p:
print(p.map(f, [1, 2, 3]))
但是如果 f(x)
更改为 f(x,y,z)
这样
def f(x, y, z):
return x*y*z
从 p.map
方法向 f(x, y, z)
传递 3 个参数的语法是什么?
使用p.starmap()
,正是针对这种情况。
也许您正在寻找 starmap
?
starmap(func, iterable[, chunksize])
Like map() except that the elements of the iterable are expected to be iterables that are unpacked as arguments.
Hence an iterable of
[(1,2), (3, 4)]
results in[func(1,2), func(3,4)]
.New in version 3.3.