首页 > 解决方案 > 遍历数组多线程

问题描述

我在找出如何编写以下多线程代码时遇到了一些困难。我想它可能只是一个语法问题。

我想要的是处理每个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))

标签: pythonmultiprocessing

解决方案


您实际上没有使用您创建的池。此外,在多处理中使用 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 预期的逻辑。可能还可以在类级别定义您的数据框。


推荐阅读