dask.multiprocessing 或 pandas + multiprocessing.pool:有什么区别?
dask.multiprocessing or pandas + multiprocessing.pool: what's the difference?
我正在开发一个用于财务目的的模型。我将整个 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)
为了加快处理速度,我同时对多个.hdf文件使用multiprocessing.pool到运行相同的算法,所以我对处理速度很满意(我有一个 4c/8t CPU)。但是现在我发现了Dask。
在 Dask documentation 'DataFrame Overview' 中表示:
平凡可并行化操作(快速):
- 逐元素运算:df.x + df.y, df * df
- 按行选择:df[df.x > 0]
- 位置:df.loc[4.0:10.5](这是我最感兴趣的地方)
此外,在 Dask documentation 'Use Cases' 中,他们表示:
A programmer has a function that they want to run many times on different inputs. Their function and inputs might use arrays or dataframes internally, but conceptually their problem isn’t a single large array or dataframe.
They want to run these functions in parallel on their laptop while they prototype but they also intend to eventually use an in-house cluster. They wrap their function in dask.delayed and let the appropriate dask scheduler parallelize and load balance the work.
所以我确定我遗漏了一些东西,或者可能不仅仅是一些东西。使用 multiprocessing.pool 和 dask.multiprocessing 处理许多单个 pandas 数据帧有什么区别?
你认为我应该针对我的具体情况使用 Dask 吗?谢谢大家。
没有区别。 Dask 正在做您在自定义代码中所做的事情。它使用 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)
为了加快处理速度,我同时对多个.hdf文件使用multiprocessing.pool到运行相同的算法,所以我对处理速度很满意(我有一个 4c/8t CPU)。但是现在我发现了Dask。
在 Dask documentation 'DataFrame Overview' 中表示:
平凡可并行化操作(快速):
- 逐元素运算:df.x + df.y, df * df
- 按行选择:df[df.x > 0]
- 位置:df.loc[4.0:10.5](这是我最感兴趣的地方)
此外,在 Dask documentation 'Use Cases' 中,他们表示:
A programmer has a function that they want to run many times on different inputs. Their function and inputs might use arrays or dataframes internally, but conceptually their problem isn’t a single large array or dataframe.
They want to run these functions in parallel on their laptop while they prototype but they also intend to eventually use an in-house cluster. They wrap their function in dask.delayed and let the appropriate dask scheduler parallelize and load balance the work.
所以我确定我遗漏了一些东西,或者可能不仅仅是一些东西。使用 multiprocessing.pool 和 dask.multiprocessing 处理许多单个 pandas 数据帧有什么区别?
你认为我应该针对我的具体情况使用 Dask 吗?谢谢大家。
没有区别。 Dask 正在做您在自定义代码中所做的事情。它使用 pandas 和一个线程或多处理池来实现并行。
出于某些原因,您可能更喜欢 Dask
- 它会想出如何自动编写并行算法
- 您将来可能希望扩展到集群
但是如果你有什么适合你那么我会坚持下去。