PHP Fiber Loop
- PHP 8.1 or later
composer require initphp/fiber-loops
require_once "vendor/autoload.php";
use InitPHP\FiberLoops\Loop;
$loop = new Loop();
$loop->defer(function () use ($loop) {
foreach (range(0, 5) as $value) {
echo $value . PHP_EOL;
$loop->next();
}
});
$loop->defer(function () use ($loop) {
foreach (range(6, 9) as $value) {
echo $value . PHP_EOL;
$loop->next();
}
});
$loop->run();
Output :
0
6
1
7
2
8
3
9
4
5
Example 2 :
require_once "vendor/autoload.php";
use InitPHP\FiberLoops\Loop;
$loop = new Loop();
$loop->defer(function () use ($loop) {
$loop->sleep(0.2);
foreach (range(0, 5) as $value) {
echo $value . PHP_EOL;
}
});
$loop->defer(function () use ($loop) {
foreach (range(6, 9) as $value) {
echo $value . PHP_EOL;
}
});
$loop->run();
Output :
6
7
8
9
0
1
2
3
4
5
Copyright © 2022 MIT License