首页 > 解决方案 > concurrent.futures.ProcessPoolExecutor 非常慢的并行化 python

问题描述

我正在尝试在 python 中并行化一个简单的函数,如下所示:

import numpy as np
import math
import concurrent.futures

def f(x):
    return x * math.sin(x) + x * x * math.cos(x)

xs = np.random.normal(0, 1, 100000)

#This takes about a second
ans1 = map(f, xs)

#This ran about 30 minutes before I gave up
with concurrent.futures.ProcessPoolExecutor() as executor:
    ans2 = executor.map(f, xs)

我知道这个问题对于并行化可能太小而无法真正有效,但我希望这个并行化版本需要几秒钟,而不是 30 多分钟。这里出了什么问题?

标签: pythonnumpyparallel-processingconcurrent.futures

解决方案


推荐阅读