Thread
#prefect-community
    Tony Yun

    Tony Yun

    11 months ago
    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.
    Kevin Kho

    Kevin Kho

    11 months ago
    You can
    raise ENDRUN
    . This will avoid any retries. Docs . Is this what you meant?
    Tony Yun

    Tony Yun

    11 months ago
    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:
    with xxx as flow:
    
        a = taskA()
        # how to kill task a as a task?
    Kevin Kho

    Kevin Kho

    11 months ago
    You mean task A calls something like
    RunNamespacedJob
    that spins up new compute like this:
    @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
    Tony Yun

    Tony Yun

    11 months ago
    yes that is right^
    Kevin Kho

    Kevin Kho

    11 months ago
    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
    Tony Yun

    Tony Yun

    11 months ago
    if I can get some metadata from
    RunNamespacedJob().run()
    like task_id, I can call something like
    client.set_task_run_state()
    Kevin Kho

    Kevin Kho

    11 months ago
    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.
    Tony Yun

    Tony Yun

    11 months ago
    I see. Yeah I need to take a different approach. Thanks @Kevin Kho ! Have a good weekend.