我正在尝试利用Python3
中的concurrent.futures.ProcessPoolExecutor
来并行处理大矩阵。代码的一般结构是:为什么concurrent.futures.ProcessPoolExecutor的性能很低?
class X(object):
self.matrix
def f(self, i, row_i):
<cpu-bound process>
def fetch_multiple(self, ids):
with ProcessPoolExecutor() as executor:
futures = [executor.submit(self.f, i, self.matrix.getrow(i)) for i in ids]
return [f.result() for f in as_completed(futures)]
self.matrix
是一个大scipy csr_matrix。 f
是我的concurrrent函数,它需要一行self.matrix
并对其应用CPU上的进程。最后,fetch_multiple
是并行运行多个实例f
并返回结果的函数。
的问题是,在运行脚本之后,所有的CPU核心均小于50%,忙(见下截图):
为什么所有的内核是不是很忙?
我认为问题是self.matrix
的大对象,并在进程之间传递行向量。我怎么解决这个问题?