Child Workflows - Python SDK
This page shows how to do the following:
Start a Child Workflow Execution
How to start a Child Workflow Execution using the Temporal Python SDK.
A Child Workflow Execution is a Workflow Execution that is scheduled from within another Workflow using a Child Workflow API.
When using a Child Workflow API, Child Workflow related Events (StartChildWorkflowExecutionInitiated, ChildWorkflowExecutionStarted, ChildWorkflowExecutionCompleted, etc...) are logged in the Workflow Execution Event History. The Child Workflow needs to have an Abandon Parent Close Policy set in the Child Workflow Options.
You should block progress on the Parent Workflow until the ChildWorkflowExecutionStarted Event is logged to the Event History to ensure the Child Workflow Execution has started. If the Parent makes the ExecuteChildWorkflow call and then immediately completes, the Child Workflow Execution doesn't start. In Python, you can make sure your Child Workflow started by awaiting start_child_workflow.
To spawn a Child Workflow Execution in Python, use the execute_child_workflow() function which starts the Child Workflow and waits for completion or
use the start_child_workflow() function to start a Child Workflow and return its handle.
This is useful if you want to do something after it has only started, or to get the Workflow/Run ID, or to be able to signal it while running.
execute_child_workflow() is a helper function for start_child_workflow() plus await handle.
View the source code
in the context of the rest of the application code.
# ...
@workflow.defn
class ComposeGreetingWorkflow:
@workflow.run
async def run(self, input: ComposeGreetingInput) -> str:
return f"{input.greeting}, {input.name}!"
@workflow.defn
class GreetingWorkflow:
@workflow.run
async def run(self, name: str) -> str:
return await workflow.execute_child_workflow(
ComposeGreetingWorkflow.run,
ComposeGreetingInput("Hello", name),
id="hello-child-workflow-workflow-child-id",
# ...
)
Set a Parent Close Policy
How to set a Parent Close Policy
A Parent Close Policy determines what happens to a Child Workflow Execution if its Parent changes to a Closed status (Completed, Failed, or Timed Out).
The default Parent Close Policy option is set to terminate the Child Workflow Execution.
Set the parent_close_policy parameter inside the start_child_workflow function or the execute_child_workflow() function to specify the behavior of the Child Workflow when the Parent Workflow closes.
View the source code
in the context of the rest of the application code.
from temporalio.workflow import ParentClosePolicy
# ...
# ...
@workflow.defn
class ComposeGreetingWorkflow:
@workflow.run
async def run(self, input: ComposeGreetingInput) -> str:
return f"{input.greeting}, {input.name}!"
@workflow.defn
class GreetingWorkflow:
@workflow.run
async def run(self, name: str) -> str:
return await workflow.execute_child_workflow(
ComposeGreetingWorkflow.run,
ComposeGreetingInput("Hello", name),
id="hello-child-workflow-workflow-child-id",
parent_close_policy=ParentClosePolicy.ABANDON,
)