Re: [VOTE] Fibers

From: Date: Thu, 18 Mar 2021 16:03:25 +0000
Subject: Re: [VOTE] Fibers
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14  Groups: php.internals 
Request: Send a blank email to internals+get-113604@lists.php.net to get a copy of this message
On Thu, Mar 18, 2021 at 1:55 PM Guilliam Xavier
<guilliam.xavier@gmail.com> wrote:
>
> Hi,
>
>> Guzzle's requestAsync() returns a promise, it does not
>> interrupt/suspend the callsite. For example:
>>
>>     // this function is not interrupted/suspended -- it will return
>> synchronously
>>     function doSomething() {
>>         $this->guzzle->requestAsync(...)->then(... handle response here ...);
>>         echo "This will be printed before a response is received";
>>     }
>>
>> The following is similar to how one would implement an coroutine in
>> PHP today when using a library which implements a coroutine mechanism,
>> such as amphp. Note the yield statement which interrupts/suspends the
>> function:
>>
>>     // this function will be suspended while the request is in flight
>> -- it will return after a response is received
>>     function doSomething()
>>         $response = yield $this->guzzle->requestAsync(...);
>>         // do something with response here
>>         echo "This will be printed AFTER the response comes back";
>>     }
>>
>> Note the difference between the two. Also note how, in both of the
>> above cases, the asynchronicity is explicit and the developer has
>> opted into it. Both of the above are different approaches to that
>> being proposed in this RFC (this is a design choice by the authors).
>
>
> At the risk of sounding dumb: What would that code look like with fibers?
>
> Thanks,
>
> --
> Guilliam Xavier

Hi Guilliam,

With fibers, a non-blocking call to Guzzle would look exactly the same
as a blocking call.

     function doSomething()
         $response = $this->guzzle->request(...);
         // do something with response here
         echo "This will always be printed AFTER the response comes back";
     }

Whether request() and therefore (doSomething()) blocks the whole
process or merely suspends the current fiber would depend on whether
or not doSomething() was called within a fiber, and whether Guzzle had
fiber support, which would be defined elsewhere.

Cheers


Thread (71 messages)

« previous php.internals (#113604) next »