Skip to content

Commit

Permalink
improvements on examples
Browse files Browse the repository at this point in the history
Signed-off-by: Demin Yin <deminy@deminy.net>
  • Loading branch information
deminy committed Dec 14, 2023
1 parent da9f7c2 commit 4ae7cb5
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/csp/coroutines/creation-2.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
/**
* This example shows how to use different types of callbacks when creating coroutines.
*
* In this example, each time when a new coroutine is created, we purposely make it sleep for 0.1 second. Thus, when
* executing the script, there will be execution switches between coroutines, and the numbers printed out could be in
* a different order. Give it a try and see what the output looks like.
*
* How to run this script:
* docker compose exec -t client bash -c "./csp/coroutines/creation-2.php"
*
Expand All @@ -19,6 +23,7 @@
// Define a callback function.
function callbackFunction(int $i): void
{
Coroutine::sleep(0.1); // Sleep for 0.1 second.
echo $i;
}

Expand All @@ -27,11 +32,13 @@ class callbackClass
{
public function callbackMethod(int $i): void
{
Coroutine::sleep(0.1); // Sleep for 0.1 second.
echo $i;
}

public static function staticCallbackMethod(int $i): void
{
Coroutine::sleep(0.1); // Sleep for 0.1 second.
echo $i;
}
}
Expand All @@ -41,6 +48,7 @@ class InvokableClass
{
public function __invoke(int $i): void
{
Coroutine::sleep(0.1); // Sleep for 0.1 second.
echo $i;
}
}
Expand All @@ -49,6 +57,7 @@ public function __invoke(int $i): void
// Type 1: A simple callback function defined directly within the code block.
Coroutine::create(
function (int $i): void {
Coroutine::sleep(0.1); // Sleep for 0.1 second.
echo $i;
},
1
Expand Down

0 comments on commit 4ae7cb5

Please sign in to comment.