Kon*_*tin 4 python pandas python-multiprocessing pyarrow
我想将共享 DataFrame 的只读访问权限授予由multiprocessing.Pool.map().
我想避免复制和酸洗。
我知道可以使用 pyarrow 。但是,我发现他们的文档非常繁琐。任何人都可以提供一个关于如何完成的例子吗?
在这个例子https://github.com/apache/arrow/blob/master/python/examples/plasma/sorting/sort_df.py是一个工作的例子,股使用Python多处理(注意,它需要多个工人之间的熊猫数据帧您构建一个小型 Cython 库以运行它)。
数据帧通过Arrow 的 Plasma 对象存储共享。
如果您不依赖 Python 多处理,您可以使用Ray以更简单的语法做您想做的事。
要为多个工作人员提供对 Pandas 数据帧的只读访问权限,您可以执行以下操作。
import numpy as np
import pandas
import ray
ray.init()
df = pandas.DataFrame(np.random.normal(size=(1000, 10)))
@ray.remote
def f(df):
# This task will run on a worker and have read only access to the
# dataframe. For example, "df.iloc[0][0] = 1" will raise an exception.
try:
df.iloc[0][0] = 1
except ValueError:
pass
return df.iloc[0][0]
# Serialize the dataframe with pyarrow and store it in shared memory.
df_id = ray.put(df)
# Run four tasks that have access to the dataframe.
result_ids = [f.remote(df_id) for _ in range(4)]
# Get the results.
results = ray.get(result_ids)
Run Code Online (Sandbox Code Playgroud)
请注意,该行df_id = ray.put(df)可以省略(您可以直接调用f.remote(df))。在这种情况下,df仍将存储在共享内存中并与工作人员共享,但将存储 4 次(每次调用 一次f.remote(df)),效率较低。
| 归档时间: |
|
| 查看次数: |
2956 次 |
| 最近记录: |