Nikhil Joseph
12/13/2023, 9:26 AMMarvin
12/13/2023, 9:26 AMcreate_flow_run
function from the prefect.client
module to create a new flow run, and then use a method like get_flow_run_info
to poll the status of the run until it's completed.
Here is a conceptual example in python pseudocode:
python
from prefect import flow
from prefect.client import create_flow_run, get_flow_run_info
from prefect.orion.schemas.states import Completed
@flow
def trigger_flow_and_wait(deployment_id):
flow_run_id = create_flow_run(deployment_id=deployment_id)
# Wait for the flow run to complete
flow_run_info = get_flow_run_info(flow_run_id=flow_run_id)
while not isinstance(flow_run_info.state, Completed):
# Sleeping is often a bad practice; you'd want to implement some
# delay with exponential backoff and perhaps a timeout to prevent
# infinite loops.
time.sleep(10)
flow_run_info = get_flow_run_info(flow_run_id=flow_run_id)
return flow_run_info.state
# Use the deployment ID of the flow you want to trigger
trigger_flow_and_wait(deployment_id="deployment-id-goes-here")
For a more accurate and detailed implementation, I would recommend reviewing the Prefect documentation or any updated examples provided by the Prefect 2.0 community, since the precise APIs and best practices can evolve.Nikhil Joseph
12/13/2023, 10:05 AMMarvin
12/13/2023, 10:06 AMNikhil Joseph
12/13/2023, 10:34 AMNate
12/13/2023, 6:16 PMrun_deployment
is a better answerMarvin
12/13/2023, 6:16 PMrun_deployment
is a function that you can use to create a flow run from a deployment in a Python script. Here's an example of how you can use `run_deployment`:
python
from prefect.deployments import run_deployment
def main():
response = run_deployment(name="flow-name/deployment-name")
print(response)
if __name__ == "__main__":
main()
Using this function, you can programmatically trigger a flow run for a specific deployment. It requires you to specify the combination of the flow name and the deployment name. Remember that, when using agents and work pools in remote environments, you also need to ensure the PREFECT_API_URL
environment variable is set to enable the agent to communicate with Prefect Cloud or a remote execution environment, such as a VM or Docker container.
For more details, you can refer to the documentation here.