python - 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 多分钟。这里出了什么问题?
解决方案
推荐阅读
- python - 为什么 matplotlib 在 jupyter notebook 中显示的绘图与导出为 pdf 时不同
- caching - 对于缓存,您如何确定要写入高度关联集的哪个块?
- android - Android 如何在外部点击时将文本保留在 EditText 中?
- xml - 如何在 Vue Js 中嵌入我的 XML RSS 文件?
- angular - 如何在单选按钮组的更改事件中更改作为模板驱动表单一部分的选定单选按钮?
- python - 在 Python 中将字符串内核包装到 scikit SVM 分类器中
- angular - 尝试设置值时未定义接口属性?
- ruby-on-rails - 评论正在保存到数据库,但未正确显示
- python - 在 Windows 10 上使用 pip 安装 pytorch 时出错
- batch-file - 批处理文件 IF 语句评估问题