Coder Social home page Coder Social logo

fiber-ext's Introduction

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

fiber-ext's People

Contributors

dstogov avatar weltling avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.