快速的异步处理,在结果到达时打印

时间:2018-06-21 18:41:39

标签: python asynchronous cluster-computing dask

以下示例不起作用,除非asynchronous中未使用Localcluster关键字。我想控制使用多少个流程/工人和并行处理功能,并在准备就绪时打印结果。需要更改什么?

import time
from dask.distributed import Client, LocalCluster, as_completed


def wait(sec):
    time.sleep(sec)
    return sec


def main():

    cluster = LocalCluster(n_workers=2, ncores=2, asynchronous=True)
    inputs = [5, 7, 3, 1]
    client = Client(cluster)
    futures = client.map(wait, inputs)

    for future, result in as_completed(futures, with_results=True):
        print(result)

    client.close()


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

根据您的建议,应该从LocalCluster调用中删除asynchronous=关键字。此关键字用于支持异步功能,如下所示:

async def main():
    cluster = await LocalCluster(n_workers=2, ncores=2, asynchronous=True)
    inputs = [5, 7, 3, 1]
    client = await Client(cluster, asynchronous=True)
    futures = client.map(wait, inputs)

    async for future, result in as_completed(futures, with_results=True):
        print(result)

    await client.close()

如果您不想使用async-await语法(这种情况比较少见),则应该忽略异步=关键字。它可能没有按照您的想象做。