我试图将关键字参数传递给
Python的multiprocessing.Pool实例中的map函数.
from multiprocessing import Pool
from functools import partial
import sys
# Function to multiprocess
def func(a, b, c, d):
print(a * (b + 2 * c - d))
sys.stdout.flush()
if __name__ == '__main__':
p = Pool(2)
# Now, I try to call func(a, b, c, d) for 10 different a values,
# but the same b, c, d values passed in as keyword arguments
a_iter = range(10)
kwargs = {'b': 1, 'c': 2, 'd': 3}
mapfunc = partial(func, **kwargs)
p.map(mapfunc, a_iter)
输出是正确的:
0
2
4
6
8
10
12
14
16
18
这是最好的做法(最“pythonic”方式)吗?我觉得:
1)常用游泳池;
2)常用关键字参数;
3)但是像上面我的例子这样的组合使用有点像实现这个目的的