You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/features/concurrency.md
+45Lines changed: 45 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,8 @@
2
2
sidebar_position: 8
3
3
---
4
4
5
+
import ConcurrencySimulator from '@site/src/components/ConcurrencySimulator';
6
+
5
7
# Concurrency
6
8
7
9
Activities can be executed in series or in parallel. In either case, you start by using `activity()` to create a new instance of an activity and return a promise that represents the execution of that activity. The activity will immediately begin executing in the background. You can then `yield` this promise to pause the execution of the workflow and wait for the result of the activity, or pass the promise into the `all()` method to wait for a group of activities to complete in parallel.
@@ -27,6 +29,16 @@ class MyWorkflow extends Workflow
27
29
}
28
30
```
29
31
32
+
<ConcurrencySimulator
33
+
activities={[
34
+
{ name: 'MyActivity1', duration: 1500 },
35
+
{ name: 'MyActivity2', duration: 2000 },
36
+
{ name: 'MyActivity3', duration: 1200 },
37
+
]}
38
+
mode="series"
39
+
title="Series Execution Simulator"
40
+
/>
41
+
30
42
## Parallel
31
43
32
44
This example will execute 3 activities in parallel, waiting for the completion of all activities and collecting the results.
@@ -48,6 +60,16 @@ class MyWorkflow extends Workflow
48
60
}
49
61
```
50
62
63
+
<ConcurrencySimulator
64
+
activities={[
65
+
{ name: 'MyActivity1', duration: 2000 },
66
+
{ name: 'MyActivity2', duration: 1500 },
67
+
{ name: 'MyActivity3', duration: 2500 },
68
+
]}
69
+
mode="parallel"
70
+
title="Parallel Execution Simulator"
71
+
/>
72
+
51
73
The main difference between the serial example and the parallel execution example is the number of `yield` statements. In the serial example, there are 3 `yield` statements, one for each activity. This means that the workflow will pause and wait for each activity to complete before continuing to the next one. In the parallel example, there is only 1 `yield` statement, which wraps all of the activities in a call to `all()`. This means that all of the activities will be executed in parallel, and the workflow will pause and wait for all of them to complete as a group before continuing.
52
74
53
75
## Mix and Match
@@ -82,6 +104,19 @@ class MyWorkflow extends Workflow
82
104
83
105
Activity 1 will execute and complete before any other activities start. Activities 2 and 3 will execute in series, waiting for each to complete one after another before continuing. At the same time, activities 4 and 5 will execute together in parallel and only when they all complete will execution continue. Finally, activity 6 executes last after all others have completed.
You can pass child workflows to `all()` along with other activities. It works the same way as parallel activity execution, but for child workflows. It allows you to fan out multiple child workflows and wait for all of them to complete together.
@@ -96,4 +131,14 @@ $results = yield all([
96
131
]);
97
132
```
98
133
134
+
<ConcurrencySimulator
135
+
activities={[
136
+
{ name: 'MyChild1', duration: 2200 },
137
+
{ name: 'MyChild2', duration: 1800 },
138
+
{ name: 'MyChild3', duration: 2500 },
139
+
]}
140
+
mode="parallel"
141
+
title="Child Workflows in Parallel Simulator"
142
+
/>
143
+
99
144
This makes it easy to build hierarchical parallelism into your workflows.
0 commit comments