hi, is there a way to kill task in flow context? I...
# ask-community
t
hi, is there a way to kill task in flow context? I know how to start a task in there, fairly easy. But not to kill a started one.
k
You can
raise ENDRUN
. This will avoid any retries. Docs . Is this what you meant?
t
not really. For example, if I have a task A, inside of it, it’s calling another task B. But when I cancel the flow, the task A is cancelled. but task B is still running on background. I want a command to kill task B at task A’s finally statement.
or ask in another scenario, I have:
Copy code
with xxx as flow:

    a = taskA()
    # how to kill task a as a task?
k
You mean task A calls something like
RunNamespacedJob
that spins up new compute like this:
Copy code
@task
def task_a():
    RunNamespacedJob().run()
    return
In this case,
RunNamespacedJob
is not a real task and in general, Prefect is currently not able to close hardware through cancelletation
t
yes that is right^
k
Yeah in this case, there is no way I think. Cancelling
task_a
mid flow run through the flow is very hard. Because it must have already executed by the time downstream tasks are reached
t
if I can get some metadata from
RunNamespacedJob().run()
like task_id, I can call something like
client.set_task_run_state()
k
Ah then in that case, you can’t get metadata from
RunNamespacedJob
from a downstream task unless you persist it in some location that can be retrieved from downstream. If you are getting the metadata in the same task,you would need a modified task that allows you to poll the NamespacedJob or something or check the logs, and then you can have a conditional that
raise SUCCESS
or
raise FAIL
. These would be better than the
client.set_task_run_state()
if the logic is in the same task.
t
I see. Yeah I need to take a different approach. Thanks @Kevin Kho ! Have a good weekend.