How do I stop a running task in Dask?

坚强是说给别人听的谎言 提交于 2019-11-28 07:31:28

问题


When using Dask's distributed scheduler I have a task that is running on a remote worker that I want to stop.

How do I stop it? I know about the cancel method, but this doesn't seem to work if the task has already started executing.


回答1:


If it's not yet running

If the task has not yet started running you can cancel it by cancelling the associated future

future = client.submit(func, *args)  # start task
future.cancel()                      # cancel task

If you are using dask collections then you can use the client.cancel method

x = x.persist()   # start many tasks 
client.cancel(x)  # cancel all tasks

If it is running

However if your task has already started running on a thread within a worker then there is nothing that you can do to interrupt that thread. Unfortunately this is a limitation of Python.

Build in an explicit stopping condition

The best you can do is to build in some sort of stopping criterion into your function with your own custom logic. You might consider checking a shared variable within a loop. Look for "Variable" in these docs: http://dask.pydata.org/en/latest/futures.html

from dask.distributed import Client, Variable

client = Client()
stop = Varible()
stop.put(False)

def long_running_task():
    while not stop.get():
        ... do stuff

future = client.submit(long_running_task)

... wait a while

stop.put(True)


来源:https://stackoverflow.com/questions/49203128/how-do-i-stop-a-running-task-in-dask

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!