如何通过dask显式停止正在运行/活动的任务。?
我有一个简单的任务,由dask调度程序调度,并在工作节点上运行如何通过dask显式停止正在运行/活动的任务。?,dask,dask-distributed,dask-delayed,Dask,Dask Distributed,Dask Delayed,我有一个简单的任务,由dask调度程序调度,并在工作节点上运行 我的要求是,我希望有控制权在用户需要时按需停止任务。您必须将其构建到任务中,也许可以通过显式检查循环中的分布式对象 from dask.distributed import Variable stop = Variable() stop.set(False) def my_task(): while True: if stop.get(): return else
我的要求是,我希望有控制权在用户需要时按需停止任务。您必须将其构建到任务中,也许可以通过显式检查循环中的分布式对象
from dask.distributed import Variable
stop = Variable()
stop.set(False)
def my_task():
while True:
if stop.get():
return
else:
# do stuff
future = client.submit(my_task)
# wait
stop.set(True)
你需要像这样明确的东西。任务通常在单独的线程中运行。据我所知,没有办法中断线程(尽管我很乐意了解其他情况)。@MRocklin。谢谢你的建议。。这是我围绕着明确停止运行/活动任务构建的机制。虽然下面的代码没有被重新考虑。。请追踪其背后的逻辑。。谢谢-Manoranjan(我会注意到你的回答真的很有帮助。)继续做好
import os
import subprocess
from dask.distributed import Variable, Client
from multiprocessing import Process, current_process
import time
global stop
def my_task(proc):
print("my_task..")
print("child proc::", proc)
p = None
childProcessCreated = False
while True:
print("stop.get()::", stop.get())
if stop.get():
print("event triggered for stopping the live task..")
p.terminate()
return 100
else:
if childProcessCreated == False:
print("childProcessCreated::", childProcessCreated)
p = subprocess.Popen("python sleep.py", shell=False)
childProcessCreated = True
print("subprocess p::", p, " type::", type(p))
time.sleep(1)
print("returnning with 20")
return 20
if __name__ == '__main__':
clienta = Client("192.168.1.2:8786")
print("global declaration..")
global stop
stop = Variable("name-xx", client = clienta)
stop.set(False)
future = clienta.submit(my_task, 10)
print("future::waiting for 4 sec..in client side", future)
time.sleep(3)
print("future after sleeping for sec", future)
#print("result::", future.result())
stop.set(True)
print("future after stopping the child process::", future)
print("child process should be stopped by now..")
#print("future::", future)
#print("future result::",future.result())
print("over.!")
从dask.distributed import*import time def task_code():out=None,而True:if stop.get():print(“正在运行的任务在这段时间内停止了!”,正在返回…”)返回else:print(“内部任务_code()”,get_worker())for uu in range(5):print(“任务仍在进行中!”)time.sleep(*2)out=“Hello World.”print(“现在任务结束了…”)return outclienta=Client(“192.168.1.2:8786”)stop=Variable('xx',clienta=clienta)stop.set(False)futurea=clienta.submit(任务代码)for u在范围内(3):print(“在客户端同时做其他事情…”)time.sleep(u)stop.set(True)它仍然不工作。。如果我在else:block中运行无限循环会怎么样。。即使我将变量设置为True以停止任务,循环仍将继续工作。。我我将示例代码粘贴在上面的注释中,分为两部分。请联合起来,看看你是否能提供进一步的帮助。。谢谢。不可能中断线程中正在运行的任务。这是Python的一个限制。你需要自己建造一些机器,定期检查一些状况。