On Thu, Mar 18, 2021 at 1:55 PM Guilliam Xavier
<[email protected]> 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
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php