Re: [RFC] Fibers
| From: | Aaron Piotrowski | Date: | Fri, 18 Dec 2020 05:59:12 +0000 |
| Subject: | Re: [RFC] Fibers | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-112546@lists.php.net to get a copy of this message | ||
> On Dec 17, 2020, at 4:11 PM, Saif Eddin Gmati <azjezz@protonmail.com> wrote:
>
> Hello Aaron,
>
> First, I want to say that I love this proposal and would love to see it land in the next PHP
> release, but I have one question regarding this:
>
>
>> Promises result in the “What color is your function” problem as described in the
>> introduction of the RFC. Returning promises from functions means that functions calling those
>> functions must also return promises, resulting in the entire call stack needing to return promises.
>
> Hack-Lang provides
HH\Asio\join function which allows awaiting
> Awaitables in sync code, so you are capable of running multiple async tasks concurrently without
> having to declare the entire call stack as "async" or with an "Awaitable" return
> type, isn't this possible?
>
> ```
> use namespace HH\Asio;
>
> async function async_task(): Awaitable<void> {
> await Asio\usleep(1000000);
> }
>
> <<__EntryPoint>>
> function main(): void {
> $start = microtime(true);
>
> $async = async {
> concurrent {
> await async_task();
> await async_task();
> };
>
> return 'hello';
> };
>
> $result = Asio\join($async);
>
> printf('Result: %s ( %f )', $result, microtime(true) - $start); // output
> "Result: hello ( 1.010382 )"
> }
>
> ```
>
> Regards,
>
> Saif.
>
Hi Saif,
HH\Asio\join() implements a synchronous await (I don't know the details of how its
implemented, possibly involving entering and exiting the built-in event loop), but it does not solve
the problem that functions using await need to be declared using async and
return an Awaitable. Your example declares async_task() as async, while a similar
function using the proposed fiber API would not need to change the function declaration to use
Fiber::suspend(). There's an example in the RFC using Amp\delay()
that is very similar to your code sample.
Fibers allow existing interfaces to be implemented using either sync or async I/O because the
interface does not need to change to return promises/awaitables.
Cheers,
Aaron Piotrowski