Parallel Tasks
Run two branches concurrently where the first to finish wins.
What you will learn
- How to run workflow branches in parallel using
fork - How
compete: truereturns only the first branch to finish - How to collect results from all branches using
compete: falseThe slower branch is cancelled.
Workflow
document:
dsl: 1.0.0
namespace: zigflow
name: competing-tasks
version: 0.0.1
timeout:
after:
minutes: 1
do:
- state:
export:
as:
data: ${ . }
set:
id: ${ uuid }
- race:
fork:
compete: true
branches:
- task1:
do:
- getUser:
call: http
with:
method: get
endpoint: >-
https://jsonplaceholder.typicode.com/users/2
- wait:
wait:
seconds: 2
- task2:
do:
- getUser:
call: http
with:
method: get
endpoint: >-
https://jsonplaceholder.typicode.com/users/1
- wait:
wait:
seconds: 50
Explanation
| Part | Purpose |
|---|---|
fork.compete: true | First branch to finish cancels the others |
fork.branches | Each branch runs as a child workflow |
state task | Generates a UUID before the race starts |
task1 finishes in roughly 2 seconds. task2 would take 50
seconds. Because compete: true is set, task2 is cancelled as
soon as task1 completes.
Cancelled branches are not rolled back. Any HTTP calls or side effects performed before cancellation are not undone. Only the winner's output is returned.
Non-competing fork
To run all branches and collect every result, use compete: false
(the default):
- parallel:
fork:
compete: false
branches:
- branch1:
call: http
with:
method: get
endpoint: https://jsonplaceholder.typicode.com/users/1
- branch2:
call: http
with:
method: get
endpoint: https://jsonplaceholder.typicode.com/users/2
The output is an object containing each branch's result, keyed by branch name.
How to run
-
Start a Temporal development server:
temporal server start-dev -
Start the worker:
zigflow run -f workflow.yaml -
Trigger the workflow:
temporal workflow start \
--type competing-tasks \
--task-queue zigflow \
--workflow-id race-1 -
View the result:
temporal workflow show --workflow-id race-1
Common mistakes
Expecting the cancelled branch output to be available.
With compete: true only the winner's output is returned. Cancelled
branch data is discarded.
Related pages
- Fork —
forkreference - Do — sequential execution within a branch
- Concepts — how Zigflow runs — child workflows and execution model