dask.multiprocessing或pandas + multiprocessing.pool:有什么区别?

ilp*_*omo 3 python multithreading multiprocessing pandas dask

我正在开发一种用于财务目的的模型。我将整个S&P500组件放在一个文件夹中,其中存储了许多.hdf文件。每个.hdf文件都有其自己的多索引(年-周-分钟)。

顺序代码示例(非并行化):

import os
from classAsset import Asset


def model(current_period, previous_perdiod):
    # do stuff on the current period, based on stats derived from previous_period
    return results

if __name__ == '__main__':
    for hdf_file in os.listdir('data_path'):
        asset = Asset(hdf_file)
        for year in asset.data.index.get_level_values(0).unique().values:
            for week in asset.data.loc[year].index.get_level_values(0).unique().values:

                previous_period = asset.data.loc[(start):(end)].Open.values  # start and end are defined in another function
                current_period = asset.data.loc[year, week].Open.values

                model(current_period, previous_period)
Run Code Online (Sandbox Code Playgroud)

为了加快处理过程,我使用multiprocessing.pool在多个.hdf文件上同时运行相同的算法,因此我对处理速度非常满意(我有一个4c / 8t CPU)。但是现在我发现了Dask。

Dask文档的“ DataFrame概述”中,它们指示:

几乎可并行化的操作(快速)

  • 元素运算:df.x + df.y,df * df
  • 按行选择:df [df.x> 0]
  • 位置:df.loc [4.0:10.5](这是我最感兴趣的地方

此外,在Dask文档的“用例”中,它们指示:

程序员想要在不同的输入上运行多次的功能。它们的功能和输入可能在内部使用数组或数据框,但从概念上讲,它们的问题不是单个大型数组或数据框。

他们希望在原型设计时在笔记本电脑上并行运行这些功能,但他们最终还打算使用内部集群。他们将其函数包装在dask.delayed中,并让适当的dask调度程序并行化并平衡工作。

因此,我确定我会缺少某些东西,或者可能不仅仅是一些东西。使用multiprocessing.pool和dask.multiprocessing处理许多单个熊猫数据帧有什么区别?

您认为我应该针对具体情况使用Dask吗?感谢大伙们。

MRo*_*lin 7

没有区别。Dask所做的只是您在自定义代码中所做的事情。它使用熊猫和线程或多处理池进行并行处理。

由于某些原因,您可能更喜欢Dask

  1. 它将找出如何自动编写并行算法
  2. 您将来可能要扩展到集群

但是,如果您所拥有的对您而言行之有效,那么我会坚持下去。