<@ULVA73B9P> The Python SDK orchestration client i...
# ask-marvin
e
@Marvin The Python SDK orchestration client is able to delete flow runs, but I want to cancel flow runs so that the information on what is run is not lost. Can you give me an example for this?
m
I'm afraid that example doesn't provide any information on how to cancel a flow run. Let me correct that for you. Please stand by while I fetch the relevant example. It seems that the provided code samples are not exactly what we need. They do not demonstrate how to cancel a flow run. I apologize for the oversight. I'll take a different approach to gather the correct information for you. Please bear with me for a moment. To cancel a flow run, you may use any of the following methods: the CLI, UI, REST API, or Python client. When cancellation is requested, the flow run is moved to a "Cancelling" state, and the worker or agent will attempt to terminate the run. Here's a brief snippet from the documentation on how cancellation is handled:
Copy code
plaintext
When cancellation is requested, the flow run is moved to a "Cancelling" state. If the deployment is a work pool-based deployment with a worker, then the worker monitors the state of flow runs and detects that cancellation has been requested. The worker then sends a signal to the flow run infrastructure, requesting termination of the run.
Please note that enhanced cancellation can be enabled with the following setting:
Copy code
shell
prefect config set PREFECT_EXPERIMENTAL_ENABLE_ENHANCED_CANCELLATION=True
Unfortunately, the specific Python example of flow run cancellation was not retrieved, but you can find more details in the Prefect documentation on flows. It is recommended to use the Prefect Client in the Python SDK for cancellation. A general approach would be something like this:
Copy code
python
from prefect import Client

client = Client()
client.cancel_flow_run(flow_run_id="your-flow-run-id")
Please replace
"your-flow-run-id"
with the actual ID of the flow run you wish to cancel. If you need further assistance, don't hesitate to let me know.
e
@Marvin however in Prefect 2, client.cancel_flow_run is not available to the PrefectClient. Is there a way to use update_flow_run to cancel a run?
m
Regrettably, the search did not provide a direct example of using
update_flow_run
to cancel a flow run in Prefect 2.x. Nonetheless, in Prefect 2.x, the typical approach to cancel a flow run programmatically would involve setting the state of the flow run to 'Cancelled'. This can be achieved by updating the flow run's state using the
set_flow_run_state
function available in the
prefect
module. Here is a Python code snippet on how you might cancel a flow run in Prefect 2.x using this function:
Copy code
python
from prefect import get_client
from prefect.orion.schemas.states import State, StateType

async with get_client() as client:
    await client.set_flow_run_state(
        flow_run_id="your-flow-run-id",
        state=State(
            type=StateType.CANCELLED,
            message="Flow run cancelled by user."
        )
    )
Ensure you replace
"your-flow-run-id"
with the actual ID of your flow run. This snippet uses asynchronous I/O, and so it should be run in an asynchronous context. For more information or if you encounter any issues, I recommend referring to the Prefect documentation or asking in the community for further assistance.