This topic describes Parallel states and provides related examples.
Overview
Parallel states are used to execute multiple child states in parallel. A Parallel state defines multiple branches, each of which contains a series of child states.
When a Parallel state is executed, the child states contained in all branches are concurrently executed. After all branches are executed, a map[string]any output that contains the results of all branches is returned by default. Then, the system uses the output constructor to further process the results.
You can configure up to 50 branches for a Parallel state.
The following table describes the attributes that a Parallel state contains.
Attribute | Data type | Required | Description | Example |
Name | string | Yes | The name of the state. | my-state-name |
Description | string | No | The description of the state. | describe it here |
Type | string | Yes | The type of the state. | Parallel |
InputConstructor | map[string]any | No | The input constructor. | See Inputs and outputs. |
Branches | ParallelBranch | Yes | The branches to be executed in parallel. | See ParallelBranch. |
OutputConstructor | map[string]any | No | The output constructor. | See the State OutputConstructor section of the "Inputs and outputs" topic. |
Next | string | No | The next state that is executed after the current state is complete. If the End attribute is true, you do not need to specify this attribute. | my-next-state |
End | bool | No | Specifies whether to end the current scope. | true |
Retry | Retry | No | The information about the retry policy. | See Error handling. |
Catch | Catch | No | The information about the catch policy. | See Error handling. |
ParallelBranch
Attribute | Data type | Required | Description | Example |
States | array | Yes | The array of states that are contained in the workflow. | See the Use case section of this topic. |
StartAt | string | Yes | The state from which the execution of the workflow starts. | my start task |
Use case
The following sample workflow defines a Parallel state. The Parallel state contains two branches, and each branch contains a Pass child state.
Type: StateMachine
Name: my-wkfl
SpecVersion: v1
StartAt: Parallel1
States:
- Type: Parallel
Name: Parallel1
End: true
Branches:
- StartAt: Pass1
States:
- Type: Pass
Name: Pass1
End: true
OutputConstructor:
FieldA: 123
- StartAt: Pass2
States:
- Type: Pass
InputConstructor:
FieldA: 321
Name: Pass2
End: true
The following code shows the output of the Pass1 child state.
{ "FieldA": 123 }
The following code shows the output of the Pass2 child state.
{ "FieldA": 321 }
The following code shows the output of the Parallel1 state. Default names are assigned to the branches. The output is constructed in the format of Branch<Index>.
{ "Branch0": { "FieldA": 123 }, "Branch1": { "FieldA": 321 } }