Dask以编程方式启动远程工作者

时间:2020-01-19 14:33:38

标签: python dask dask-distributed

我需要以编程方式创建远程工作者并将其用于任务,然后将其关闭。

文档中给出的示例代码可以很好地满足以下要求:

import asyncio
from distributed import Worker, Scheduler, Client
from distributed.scheduler import WorkerState

s = "x.x.x.x:8786" # remote IP, not local, started from command line.

async def f():
    async with Worker(s) as w1, Worker(s) as w2:
        async with Client(s, asynchronous=True) as client:
            future = client.submit(lambda x: x + 1, 10)
            result = await future
            print(result)

asyncio.get_event_loop().run_until_complete(f())

假设我有n以外的机器,而不是dask调度程序-ip1, ip2, ..ipn。现在,我面临着两个问题:

  1. 连接到远程调度程序后,我想在多台计算机上创建工作线程。假设ip1, ip2, ip3。在创建host时同时使用了contact_addressWorker参数。工作人员从调度程序的本地本身开始,但不在所需的计算机中。如何在连接到同一调度程序的所需计算机中远程启动工作程序?
  2. 我需要在client函数中创建的async才能随时间用于多个submitmap调用中。我也有很多自定义的python函数。因此,我该如何以编程方式在不同的计算机上创建工作线程,如何创建client并在异步功能之外逐步使用它。我尝试了以下操作,但未成功。

s_address = "x.x.x.x:8786" # remote scheduler IP

async def f():
    async with Worker(s_address) as w1, Worker(s_address) as w2:
        async with Client(s_address, asynchronous=True) as client:
            return client

client_to_use = f() # expecting client object which I can use and...
                    # ...when everything finishes, hoping context manager kills the workers.
                    # This clearly doesn't work
asyncio.get_event_loop().run_until_complete(f()) # not sure if this is valid anymore

# What I need to do
custom_module.call_some_fn_to_use_dask_client(client_to_use) # Does not work as well!! ```

1 个答案:

答案 0 :(得分:0)

您应该阅读setup dask可用的各种选项。简而言之,您需要一种方法来与要在其上启动工作程序的机器进行对话。调度程序不知道如何执行此操作,本地客户端也不知道如何执行此操作,您需要自己选择机制。它可以像登录到远程机器并启动工作进程(即运行python)一样简单,但是有一些更复杂的系统,例如超级计算机调度程序,yarn和kubernetes。

Worker的文档清楚地表明,您是在呼叫发生的此处实例化的。

在进一步研究之前,您可能应该考虑要达到的目标,然后对它进行整理……

相关问题