Eric
02/29/2024, 6:04 PMMarvin
02/29/2024, 6:04 PMplaintext
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:
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:
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.Eric
02/29/2024, 8:40 PMMarvin
02/29/2024, 8:41 PMupdate_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:
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.