-
Notifications
You must be signed in to change notification settings - Fork 24
/
coroutine-yielded-3.php
executable file
·37 lines (32 loc) · 1.29 KB
/
coroutine-yielded-3.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env php
<?php
declare(strict_types=1);
/**
* This example shows how deadlock happens when the only coroutine yields its execution. There is no any other coroutine
* to execute, and the coroutine never gets resumed. Inside that coroutine, whatever code after the yield statement will
* never be executed.
*
* This example sets a customized exit condition so that the program will finish its execution after all coroutines
* finish execution. The program will never exit since the exit condition won't meet.
*
* How to run this script:
* docker compose exec -t client bash -c "./csp/deadlocks/coroutine-yielded-3.php"
*/
use Swoole\Constant;
use Swoole\Coroutine;
Coroutine::set(
[
Constant::OPTION_EXIT_CONDITION => function () {
return Coroutine::stats()['coroutine_num'] === 0; // @phpstan-ignore offsetAccess.nonOffsetAccessible
},
]
);
Coroutine::create(function () {
echo '1', PHP_EOL; // This will be printed out.
Coroutine::yield();
echo '3', PHP_EOL; // This will never be printed out.
});
echo '2', PHP_EOL; // This will be printed out.
// NOTE: In most cases it's not necessary nor recommended to use method `Swoole\Event::wait()` directly in your code.
// The example in this file is just for demonstration purpose.
Swoole\Event::wait();