python - 遍历数组多线程
问题描述
我在找出如何编写以下多线程代码时遇到了一些困难。我想它可能只是一个语法问题。
我想要的是处理每个col
并行,每个 colvfc
在同一个data
对象中都有它们的 - 数组。
提前致谢
with multiprocessing.Pool() as pool:
for col in list_column_names:
# returns an array
vfc = self.get_vfc(col)
data[vfc] = data[col].apply(lambda x: self.smth(x, self.model))
解决方案
您实际上没有使用您创建的池。此外,在多处理中使用 lambda 表达式可能会有问题,参见。 Python Multiprocessing Pool Map: AttributeError: Can't pickle local object 你可以尝试这样的事情:
with multiprocessing.Pool() as pool:
pool.map(self.cf, [data[c] for c in data])
其中函数cf
在类级别定义,并包含使用apply
和您的 lambda 预期的逻辑。可能还可以在类级别定义您的数据框。
推荐阅读
- r - 如何在 R 项目中使用 fviz_cluster 更改符号和颜色
- angular - 我们如何使用角垫表按姓氏排序或按字母顺序排列?
- c++ - 在迷宫中的 2 点之间找到一条最小转弯的路径
- nginx - Nginx 保留代理导航回主机
- c# - EF-Core:如何将连接表达式传递给动态 lambda 表达式?
- python - 无法从 keras.optimizer 安装 Adam
- python - 根据用户在计算器中给出的数据类型输出结果[Python]
- java - 如何通过单击表格中的单选按钮来随机选择动态值 - 无法单击单选按钮
- react-native - 我在 React-navigation V6 中自定义标题时遇到问题
- kubernetes - 使用 AWS Cloudformation 管理 Kubernetes 资源