本文介绍了如何在Dask中停止正在运行的任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Dask的分布式调度程序时,我有一个正在我要停止的远程工作者上运行的任务。

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

如何阻止它?我知道有关cancel方法的信息,但是如果任务已经开始执行,这似乎不起作用。

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.

推荐答案

尚未运行



如果任务尚未开始运行,则可以通过取消关联的将来取消任务

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

如果您使用的是Dask集合,则可以使用client.cancel方法

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



如果正在运行



但是,如果您的任务已经开始在工作线程中的线程上运行,那么您无能为力该线程。不幸的是,这是Python的局限性。

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.

最好的办法是建立某种停止条件使用您自己的自定义逻辑进入您的函数。您可以考虑在循环中检查共享变量。在这些文档中查找变量:

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)

这篇关于如何在Dask中停止正在运行的任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 14:42
查看更多