Re: add Fiber (sackful coroutine) support
| From: | Rowan Collins | Date: | Sat, 02 Sep 2017 12:19:46 +0000 |
| Subject: | Re: add Fiber (sackful coroutine) support | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-100353@lists.php.net to get a copy of this message | ||
On 1 September 2017 14:02:29 BST, Haitao Lv <i@lvht.net> wrote:
>
>Fiber is a lightweight thread. Please see
>https://en.wikipedia.org/wiki/Fiber_(computer_science)
>And ruby support Fiber. Please see
>https://ruby-doc.org/core-2.4.1/Fiber.html
Ah, thanks, that makes more sense now.
I note that the examples there all implement it not as a keyword, but as a library function, which
maybe makes more sense: whereas "yield" turns a function declaration into a generator
declaration, "Fiber\yield", as we might call it, is just a function call which can happen
anywhere and manipulates some global state.
The choice of "await" also feels odd: you're not awaiting the thing on the right-hand
side of the keyword, you're sending it somewhere and awaiting something else.
> You can see the await as a resumable return. The value after it
>will be returned and the function
>will be paused.
Again, this explanation doesn't make sense if you can call foo() without any Fiber code. If
there isn't an active Fiber to pause, the "await" line can't "return"
anything; it will presumably throw an error of some sort.
function foo($x) {
$y = Fiber\yield($x);
return $x + $y;
}
echo foo(1);
The 1 isn't "returned" to the echo, it's passed off to a global function for
further processing. This makes much more sense to me than implying that foo() is no longer a normal
function.
>> Your ->resume() doesn't seem to do anything a normal generator can't,
>except that the yield is nested deeper.
>
>The await is a nested deeper yield!
Yes, I understand that, but that seems to be completely unrelated to the API design of having
resume() act as start, continue, and final result all rolled into one. As I say, I didn't
entirely follow the reasoning for allowing return in generators, but if Fibers are used for the same
purpose, I would expect the same concerns to arise.
However, I think the comparison to generators may be more distracting than useful, as they seem to
be very different solutions to the same or related problems.
Regards,
--
Rowan Collins
[IMSoP]