python中具有多个输入和多个输出的并行函数调用

时间:2018-07-20 09:32:42

标签: python parallel-processing

我正在尝试在python中与多个输入和多个输出并行运行一个函数。为了争辩,让我们将f定义如下:

import numpy as np
def f(x,y):
    return x*x, y+x, x*y, x * np.ones((3,4,6,7))

使用地图,我可以写:

a = np.array(range(10))
b = np.eye(10)

l = list(map(f,a,b))
l = list(map(np.array,zip(*l)))

l现在是一个长度为4的数组,每个元素都是一个ndarray

但是如果我将地图替换为“平行”地图

import multiprocessing as multi
p=multi.Pool(processes=3)
m = p.map(f,a,b)

突然崩溃:

  File "tmp.py", line 13, in <module>
    m = p.map(f,a,b)
  File "/usr/lib/python3.6/multiprocessing/pool.py", line 266, in map
    return self._map_async(func, iterable, mapstar, chunksize).get()
  File "/usr/lib/python3.6/multiprocessing/pool.py", line 387, in _map_async
    error_callback=error_callback)
  File "/usr/lib/python3.6/multiprocessing/pool.py", line 669, in __init__
    if chunksize <= 0:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

我认为地图和平行地图是相同的,有人可以帮我吗?

0 个答案:

没有答案