@@ -54,7 +54,7 @@ Please use the diagrams from the table to simplify understanding.
5454
5555This **RFC** describes the **API** and **new syntax** for writing concurrent code in PHP, which includes:
5656
57- === ** Coroutine** ===
57+ === Coroutine ===
5858A lightweight execution thread that can be suspended (`suspend`) and resumed.
5959Example:
6060<code php>
@@ -143,7 +143,7 @@ echo "Next line\n";
143143
144144Output:
145145
146- </ code>
146+ <code>
147147Next line
148148File content: ...
149149</code>
@@ -188,7 +188,7 @@ echo "Next line\n";
188188
189189Output:
190190
191- </ code>
191+ <code>
192192Hello, World
193193Next line
194194Goodbye, World
@@ -450,7 +450,7 @@ will be thrown from the suspension point.
450450
451451=== Coroutine Lifecycle ===
452452
453- </ code>plantuml
453+ <code>plantuml
454454@startuml
455455state "Created" as Created
456456state "Queued" as Queued
@@ -919,7 +919,7 @@ spawn with hiPriority() {
919919
920920**Expected output:**
921921
922- </ code>
922+ <code>
923923high priority
924924normal priority
925925</code>
@@ -952,7 +952,7 @@ spawn example('Universe');
952952
953953Expected output:
954954
955- </ code>
955+ <code>
956956Hello, World!
957957Hello, Universe!
958958Goodbye, World!
@@ -1020,7 +1020,7 @@ echo "Back to the main flow";
10201020
10211021Expected output:
10221022
1023- </ code>
1023+ <code>
10241024Hello, World!
10251025Back to the main flow
10261026Goodbye, World!
@@ -1051,7 +1051,7 @@ $couroutine->cancel();
10511051
10521052**Expected output:**
10531053
1054- </ code>
1054+ <code>
10551055Hello, World!
10561056Caught exception: cancelled at ...
10571057Goodbye, World!
@@ -1078,7 +1078,7 @@ echo "Main flow";
10781078
10791079**Expected output:**
10801080
1081- </ code>
1081+ <code>
10821082Start reading file1.txt
10831083Start reading file2.txt
10841084Main flow
@@ -1356,7 +1356,7 @@ try {
13561356
13571357**Expected output:**
13581358
1359- </ code>
1359+ <code>
13601360Caught exception: Error
13611361</code>
13621362
@@ -1461,15 +1461,15 @@ $scope->awaitCompletion(Async\signal(SIGTERM));
14611461
14621462**Expected output:**
14631463
1464- </ code>
1464+ <code>
14651465Sibling task 1
14661466Sibling task 2
14671467Sibling task 3
14681468</code>
14691469
14701470**Structure:**
14711471
1472- </ code>
1472+ <code>
14731473main() ← defines a $scope
14741474└── $scope = new Scope()
14751475 ├── task1() ← runs in the $scope
@@ -1536,7 +1536,7 @@ $scope->awaitCompletion(Async\signal(SIGTERM));
15361536
15371537**Expected output:**
15381538
1539- </ code>
1539+ <code>
15401540Sibling task 1
15411541Sibling task 2
15421542Sibling task 3
@@ -1574,7 +1574,7 @@ try {
15741574
15751575**Expected output:**
15761576
1577- </ code>
1577+ <code>
15781578Error occurred
15791579</code>
15801580
@@ -1597,7 +1597,7 @@ try {
15971597
15981598**Expected output:**
15991599
1600- </ code>
1600+ <code>
16011601Caught exception: cancelled at ...
16021602</code>
16031603
@@ -1632,7 +1632,7 @@ spawn with $scope use($scope) {
16321632
16331633**Expected output:**
16341634
1635- </ code>
1635+ <code>
16361636Finally
16371637Caught exception: cancelled at ...
16381638</code>
@@ -1655,7 +1655,7 @@ A hierarchy can be a convenient way to describe an application as a set of depen
16551655* Child tasks MUST NOT control or wait for their parent tasks.
16561656* It is correct if tasks at the same hierarchy level are only connected to tasks of the immediate child level.
16571657
1658- </ code>
1658+ <code>
16591659WebServer
16601660├── Request Worker
16611661│ ├── request1 task
@@ -1672,7 +1672,7 @@ Each request may spawn subtasks. On the same level, all requests form a group of
16721672
16731673`Scope` is fit for implementing this concept:
16741674
1675- </ code>
1675+ <code>
16761676WebServer
16771677├── Request Worker
16781678│ ├── request1 Scope
@@ -1815,7 +1815,7 @@ Let's examine how this example works.
18151815 As soon as the client disconnects, `connectionChecker` will cancel all coroutines related to the request.
181618166. If the main `Scope` is closed, all coroutines handling requests will also be canceled.
18171817
1818- </ code>
1818+ <code>
18191819GLOBAL <- globalScope
18201820│
18211821├── socketListen (Scope) <- rootScope
@@ -1869,7 +1869,7 @@ $scope->cancel();
18691869
18701870**Expected output:**
18711871
1872- </ code>
1872+ <code>
18731873</code>
18741874
18751875=== Scope disposal ===
@@ -1920,7 +1920,7 @@ $scope->disposeSafely();
19201920
19211921**Expected output:**
19221922
1923- </ code>
1923+ <code>
19241924Root task
19251925Warning: Coroutine is zombie at ... in Scope disposed at ...
19261926Warning: Coroutine is zombie at ... in Scope disposed at ...
@@ -1965,7 +1965,7 @@ $scope->dispose();
19651965
19661966**Expected output:**
19671967
1968- </ code>
1968+ <code>
19691969Warning: Coroutine is zombie at ... in Scope disposed at ...
19701970Warning: Coroutine is zombie at ... in Scope disposed at ...
19711971Warning: Coroutine is zombie at ... in Scope disposed at ...
@@ -2013,7 +2013,7 @@ unset($service);
20132013
20142014**Expected output:**
20152015
2016- </ code>
2016+ <code>
20172017Task 1
20182018Warning: Coroutine is zombie at ... in Scope disposed at ...
20192019Task 2
@@ -2322,7 +2322,7 @@ $scope->awaitCompletion(Async\signal(SIGTERM));
23222322
23232323**Structure:**
23242324
2325- </ code>
2325+ <code>
23262326main() ← defines a $scope
23272327└── $scope = new Scope()
23282328 ├── task1() ← runs in the $scope
@@ -2467,7 +2467,7 @@ $scope->dispose();
24672467
24682468**Expected output:**
24692469
2470- </ code>
2470+ <code>
24712471</code>
24722472
24732473There are no warnings about **zombie coroutines** in the output
@@ -2547,7 +2547,7 @@ await $taskGroupParent;
25472547
25482548**Structure:**
25492549
2550- </ code>
2550+ <code>
25512551main()
25522552└── $taskGroupParent = new TaskGroup() <- parent task group scope
25532553 ├── $taskGroupChild = new TaskGroup(Scope::inherit()) <- child task group scope
@@ -2584,7 +2584,7 @@ $taskGroup->cancel(new \Async\CancellationException('Custom cancellation message
25842584
25852585**Expected output:**
25862586
2587- </ code>
2587+ <code>
25882588Task was cancelled: Custom cancellation message
25892589</code>
25902590
@@ -2675,7 +2675,7 @@ try {
26752675
26762676**Expected output:**
26772677
2678- </ code>
2678+ <code>
26792679Caught exception: TaskGroup was cancelled at ...
26802680</code>
26812681
@@ -3108,7 +3108,7 @@ print_r($results);
31083108
31093109Expected output:
31103110
3111- </ code>text
3111+ <code>text
31123112Array
31133113(
31143114 [0] => ... // result of fetchUserData()
@@ -3344,7 +3344,7 @@ An uncaught exception in a coroutine follows this flow:
334433447. If there is no parent scope, the exception falls into `globalScope`,
33453345 where the same rules apply as for a regular scope.
33463346
3347- </ code>
3347+ <code>
33483348puml
33493349@startuml
33503350start
@@ -3509,7 +3509,7 @@ If an exception occurs in a coroutine created within a **child Scope**,
35093509it will be passed to the `setChildScopeExceptionHandler` handler and will not affect
35103510the operation of the service as a whole.
35113511
3512- </ code>
3512+ <code>
35133513puml
35143514@startuml
35153515actor Client
@@ -3703,7 +3703,7 @@ try {
37033703
37043704Expected output:
37053705
3706- </ code>
3706+ <code>
37073707The end
37083708</code>
37093709
@@ -3732,7 +3732,7 @@ try {
37323732
37333733Expected output:
37343734
3735- </ code>
3735+ <code>
37363736Caught CancellationException
37373737The end
37383738</code>
0 commit comments