From 4ae7cb5089fb59a7529ffd5a75e5537ad7e3af35 Mon Sep 17 00:00:00 2001 From: Demin Yin Date: Wed, 13 Dec 2023 21:47:06 -0800 Subject: [PATCH] improvements on examples Signed-off-by: Demin Yin --- examples/csp/coroutines/creation-2.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/examples/csp/coroutines/creation-2.php b/examples/csp/coroutines/creation-2.php index 21fe1c5..fd34465 100755 --- a/examples/csp/coroutines/creation-2.php +++ b/examples/csp/coroutines/creation-2.php @@ -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" * @@ -19,6 +23,7 @@ // Define a callback function. function callbackFunction(int $i): void { + Coroutine::sleep(0.1); // Sleep for 0.1 second. echo $i; } @@ -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; } } @@ -41,6 +48,7 @@ class InvokableClass { public function __invoke(int $i): void { + Coroutine::sleep(0.1); // Sleep for 0.1 second. echo $i; } } @@ -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