Skip to content

InitPHP/FiberLoops

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

InitPHP Fiber Loops

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