17

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.

jtlz2
  • 7,700
  • 9
  • 64
  • 114
MRocklin
  • 55,641
  • 23
  • 163
  • 235

1 Answers1

16

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 = Variable()
stop.set(False)

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

future = client.submit(long_running_task)

... wait a while

stop.set(True)
jtlz2
  • 7,700
  • 9
  • 64
  • 114
MRocklin
  • 55,641
  • 23
  • 163
  • 235
  • What is the lifetime of a Variable? If no clients have a reference to it, is it garbage collected? Aside: for long-running tasks, I instead set meta-data using the long-running-task's key so that "If the key corresponds to a task then that key will be cleaned up when the task is forgotten by the scheduler" – sheridp Mar 23 '18 at 03:56
  • 1
    Yes, variables are cleaned up if no reference to them exists. – MRocklin Mar 23 '18 at 14:42
  • This seems to be old information. I am able to stop running tasks just by calling Future.cancel(). – SCGH Dec 03 '20 at 21:28
  • Well, I can confirm this happens. I am very surprised! – SCGH Apr 14 '21 at 01:34
  • Can someone officially confirm whether it is possible to cancel an already-running task? From my test it seems not to be able to cancel a task - at least a dummy cpu-bound one... @MRocklin? – Tomer Cagan Jul 20 '22 at 08:30
  • @SCGH, Can you share a minimal example showing this? – Tomer Cagan Jul 25 '22 at 19:33
  • @MRocklin This solution assumes that the while loop will keep looping. What if there's a line of code inside the while loop that is hanging? – Z4NG Mar 18 '23 at 00:14