Hi, All,

The generator has been introduced in PHP 5.5. And we can it pause a function 
execution.
With these feature, we can schedule multi io task in userland without blocking.

More details can be found at [Nikic].

And there is also some asynchronous frameworks like [AMP], which allow 
programer to 
code like blocking, but run asynchronously.

However, like python's generator, lua's coroutine, javascript's async/await, 
php's
generator also is a stackless coroutine implementation. We cannot pause a 
function call
in it's sub function call. This is the reason that frameworks like [AMP] has 
many wrapper,
is hard to understand, and almost impossible to be used to implement real but 
complex system.

So I propose to introduce the sackful coroutine, aka Fiber, support for PHP. 
And the possible
API like this,

> <?php
> function foo($a)
> {
>   $b = await $a + 1;
>   echo $b;
> }
> 
> function bar()
> {
>   foo();
> }
> 
> $f = new Fiber(function($a) {
>   bar($a);
>   return 3;
> });
> 
> $c = $f->resume(1);
> // 1 will be passed to lambda, then bar
> // bar call foo, and the **await** in foo paused this execution
> // and make the return value of resume as $a + 1
> // so $c is 2
> 
> $c = $f->resume(3);
> // resume the execution by previous await, and the $b in foo
> // will be assigned a value of 3
> // so foo echo 3 and return and then the lambda return
> // and the resume got a return value of 3. so $c is 3.

So the Fiber API is a little like the Generator API, but is more simple yet 
powerful. So there
is no need to distinct $generator->current(), $generator->send(), and 
$generator->getReturn().

I made a more complex example at [FIREPHP].And I also make a [PR] for comment.

All comment are welcome. Thanks.

[Nikic] 
https://nikic.github.io/2012/12/22/Cooperative-multitasking-using-coroutines-in-PHP.html
[AMP] http://amphp.org/
[FIREPHP] https://github.com/fiberphp/fiber-core/blob/master/app.php
[PR] https://github.com/php/php-src/pull/2723


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to