<@ULVA73B9P> I want to create a subflow with its o...
# ask-marvin
g
@Marvin I want to create a subflow with its own infrastructure using a separate deployment. How do I execute in using the client.create_flow_run_from_deployment and link the child flow to the parent flow in the ui. I think i need parent_task_run_id, but am not sure how to populate it
m
To create a subflow with its own infrastructure using a separate deployment in Prefect 3.x, and to link this child flow to the parent flow in the UI, you can use the
create_flow_run_from_deployment
method. Here's a concise guide: 1. Create a Deployment for the Subflow: Ensure your subflow is deployed separately. This involves creating a deployment that specifies the infrastructure and execution environment for the subflow. 2. Trigger the Subflow from the Parent Flow: - Use the
client.create_flow_run_from_deployment
method to start the subflow. This method requires the deployment ID of the subflow. - To link the child flow to the parent flow, you will need to pass the
parent_task_run_id
. This ID is a special task run in the parent flow that represents the child flow run. 3. Populate
parent_task_run_id
: - When you initiate the subflow from the parent flow, you can obtain the
parent_task_run_id
from the context of the parent flow's task run. This is typically done by accessing the current task run context within the parent flow. 4. UI Representation: In the Prefect UI, each child flow run is linked to its parent, allowing you to observe them individually. This linkage is automatically handled when you provide the
parent_task_run_id
. Here are some relevant links that might help: - Prefect Deployments Overview - Writing Flows and Handling Subflows This setup allows you to manage and observe the flow hierarchy effectively in the Prefect UI. If you need further details on accessing the task run context, let me know!
g
@Marvin I am using prefect 2.x can you give me an example of how to pull that ID from the parent?
m
In Prefect 2.x, to retrieve the
parent_task_run_id
when executing a subflow, you can access the task run context within your parent flow. Here's a concise example of how to do this: 1. Access the Task Run Context: Use the
TaskRunContext.get()
method to access the current task run context. This will allow you to retrieve the
task_run_id
. 2. Example Code:
Copy code
python
   from prefect import flow, task
   from prefect.context import TaskRunContext

   @task
   def get_parent_task_run_id():
       # Access the current task run context
       task_run_ctx = TaskRunContext.get()
       # Retrieve the task run ID
       parent_task_run_id = task_run_ctx.task_run.id
       return parent_task_run_id

   @flow
   def parent_flow():
       # Call the task to get the parent task run ID
       parent_task_id = get_parent_task_run_id()
       # Use the parent_task_run_id when triggering the subflow
       # Example: client.create_flow_run_from_deployment(..., parent_task_run_id=parent_task_id)
       print(f"Parent Task Run ID: {parent_task_id}")

   if __name__ == "__main__":
       parent_flow()
3. Trigger the Subflow: Once you have the
parent_task_run_id
, you can use it when calling
client.create_flow_run_from_deployment
to link the child flow with the parent flow in the UI. For more details, you can refer to the Prefect 2.x documentation on Accessing the Run Context. This approach ensures that the subflow is correctly linked to its parent in the Prefect UI.
g
@Marvin is this then only way to set the parent flow in a flow run?
m
In Prefect 2.x, linking a child flow to a parent flow is primarily achieved through the
parent_task_run_id
mechanism. When a flow function is called within another flow, it naturally creates a subflow, establishing a parent-child relationship. This subflow is automatically linked to the parent flow in the backend, providing a full representation as if it had been executed independently. However, if you're using separate deployments and need to explicitly set this relationship, retrieving and using the
parent_task_run_id
as described earlier is the standard approach. This ensures that the child flow is correctly associated with the parent flow in the Prefect UI. Here's a summary of the relevant points: - Subflow Creation: Automatically occurs when a flow is called within another flow, establishing a parent-child link. - Explicit Linking: Use
parent_task_run_id
when triggering subflows through separate deployments to maintain the link in the UI. For more details, you can explore the Prefect 2.x documentation on Composing Flows. This section explains the behavior and representation of subflows in the Prefect framework.