Skip to content

liexusong/fiber-ext

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP-Fiber

Fibers are primitives for implementing light weight cooperative concurrency in PHP. Basically they are a means of creating Closure that can be paused and resumed. The scheduling of fiber must be done by the programmer and not the VM.

More details can be found at this RFC, and this PR.

Install

php-fiber only support PHP 7.2+. If you use PHP-7.2, you must compile your PHP from source and this patch is needed. As that patch has been merged at 6780c74, php-fiber will work with PHP-7.3(may release in 2018).

And then, you need do this

patch -b -p1 < zend_fiber.patch # for PHP-7.2

phpize
./configure
make
sudo make install

Usage

<?php
function sub1()
{
    // yield from sub call
    return Fiber::yield(1);
}
$fiber = new Fiber(function ($a, $b) {
    $c = Fiber::yield($a + $b);

    $d = sub1();
    return $d.$c;
});

echo $fiber->resume(1, 2);     // echo 3
echo $fiber->resume("world");  // echo 1
echo $fiber->resume("hello "); // echo "hello world"

Each Fiber has a separate 4k stack. You can use the fiber.stack_size ini option to change the default stack size. You can also use the second argument of Fiber::__construct to set the stack size on fly.

Known issues

Fiber::yield cannot be used in internal callback

The following code will cause a coredump.

<?php
$f = new Fiber(function () {
    array_map(function ($i) {
        Fiber::yield($i);
    }, [1,2]);
});

$f->resume();

Roadmap

About

stackful-coroutines for PHP

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 74.9%
  • PHP 24.2%
  • M4 0.9%