我有一个函数可以有效地生成图像并将其存储到磁盘中。该函数没有参数:
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/