initphp/fiber-loops

PHP Events Loop

1.0 2022-07-13 18:15 UTC

This package is auto-updated.

Last update: 2024-10-13 23:07:35 UTC


README

PHP Fiber Loop

php-fiber

Requirements

  • PHP 8.1 or later

Installation

composer require initphp/fiber-loops

Usage

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

Credits

License

Copyright © 2022 MIT License