Using prefect client methods
Learn how to use the PrefectClient
to interact with the API.
Overview
The PrefectClient
offers methods to simplify common operations against Prefect’s REST API that may not be abstracted away by the SDK.
For example, to reschedule flow runs, one might use methods like:
read_flow_runs
with aFlowRunFilter
to read certain flow runscreate_flow_run_from_deployment
to schedule new flow runsdelete_flow_run
to delete a veryLate
flow run
Getting a client
By default, get_client()
returns an asynchronous client to be used as a context manager, but you may also use a synchronous client.
Examples
These examples are meant to illustrate how one might develop their own utilities for interacting with the API.
If you believe a client method is missing, or you’d like to see a specific pattern better represented in the SDK generally, please open an issue.
Reschedule late flow runs
To bulk reschedule flow runs that are late, delete the late flow runs and create new ones in a
Scheduled
state with a delay. This is useful if you accidentally scheduled many
flow runs of a deployment to an inactive work pool, for example.
The following example reschedules the last three late flow runs of a deployment named
healthcheck-storage-test
to run six hours later than their original expected start time.
It also deletes any remaining late flow runs of that deployment.
First, define the rescheduling function:
Then use it to reschedule flows:
Get the last N
completed flow runs from your workspace
To get the last N
completed flow runs from your workspace, use read_flow_runs
and prefect.client.schemas
.
This example gets the last three completed flow runs from your workspace:
Use it to get the last 3 completed runs:
Instead of the last three from the whole workspace, you can also use the DeploymentFilter
to get the last three completed flow runs of a specific deployment.
Transition all running flows to cancelled through the Client
Use get_client
to set multiple runs to a Cancelled
state.
This example cancels all flow runs that are in Pending
, Running
, Scheduled
, or Late
states when the script is run.
Cancel all pending, running, scheduled or late flows:
Was this page helpful?