我有一个嵌套的参数列表(基本上是一个矩阵),我想将其传递给一个耗时的函数。我想使用 multiprocess.Pool 并行化代码。如果我有一个参数列表,我会简单地调用该列表上的 Pool.map。
但是如何在矩阵上使用 Pool.map 以使结果仍然是二维矩阵格式?
假设我有一个函数 f(x) 和一个列表
l = [[1, 2, ..., n], [n + 1, ..., 2n], [(m-1)*n, ..., n*m]
如何获取列表
[[f(1), f(2), ..., f(n)], [f(n + 1), ..., f(2*n)], [f((m-1)*n), ..., f(n*m)]
通过使用进程池来并行计算 f 的计算,这是一个耗时的函数?
请您参考如下方法:
这里有一个简单的例子,只需在应用 pool.map 的矩阵上调用 map:
import multiprocessing
def add(x):
return x + 10
if __name__ == "__main__":
pool = multiprocessing.Pool() # build the process pool
l = [range(10) for _ in xrange(10)] # we build an example list
res = map(lambda x: pool.map(add, x), l) # we apply the pool.map with the map to the list
multiprocessing.freeze_support() # this line is needed on windows only
print res
这是打印的结果:
>>>[[10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]]
