python - 为Dask分布式客户端准备数据的最佳方式

标签 python python-3.x parallel-processing dask parallelism-amdahl

我有一个函数可以有效地生成图像并将其存储到磁盘中。该函数没有参数:

def generate_and_save():
    pass # generate and store image

我需要生成大量图像(比如 100k),所以我选择 Dask。阅读手册后,我整理了一个函数,该函数创建分布式(本地)客户端并使用多个进程执行任务,如下所示:

from dask.distributed import Client as DaskClient

def generate_images(how_many=10000, processes=6):
    # start Dask distributed client with 1 thread per process
    client = DaskClient(n_workers=processes, threads_per_worker=1)
    # submit future functions to cluster
    futures = []
    for i in range(how_many): 
        futures.append(client.submit(generate_and_save, pure=False))
    # execute and compute results (synchronous / blocking!)
    results = client.gather(futures)
    print(len(results))
    # stop & release client
    client.close()

generate_images(50000)

如您所见,“ future ”在 for 循环中提交到服务器并存储在一个简单的列表中。问题是:在这种情况下是否有更有效的方法来添加和执行 future ?例如,并行化提交过程本身?

最佳答案

不。这看起来不错。我不认为开销会花费太长时间,可能每个任务不到 1 毫秒,所以 10 秒

如果此开销很长,那么您可能需要阅读此文档部分:https://docs.dask.org/en/latest/best-practices.html#avoid-very-large-graphs

关于python - 为Dask分布式客户端准备数据的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57915944/

相关文章:

python - factoryboy 不使用 freezegun

python - Feedparser - KeyError : 'fullcount'

python - python中的保留方法

Python 将列表转换为数据框

c - 在 OpenCL C 中声明 cl_uint 变量会导致段错误(核心转储)

python - 如何修复 "MidiOutWinMM::openPort: error creating Windows MM MIDI output port."

python - 从 Python 中的生成器创建一个 zip 文件?

python - 带有身份验证的 urllib.request.urlopen(url)

c# Parallel.For 和 UI 更新?

c - 在 MPI 中实现 Cannons 算法