Re: async/await - is future reserved words in PHP 7.x?

php.internals

Daniel Lowrey

10 years ago
> On Sep 28, 2015, at 3:29 AM, S.A.N <ua.san.alex@gmail.com> wrote:> > Are there any future plans for - async/await?> This need to know now, not to use these words to constants, and class names...> > -- > PHP Internals - PHP Runtime Development Mailing List> To unsubscribe, visit: http://www.php.net/unsub.php>
Would this be awesome? Yes. The issue is that all of PHP's built-in IO operations are currently synchronous. It would/will require a massive effort to (1) install the infrastructure to offload blocking operations to worker threads and/or (2) convert many synchronous IO operations to a non-blocking paradigm. Someone has to do that work ... and it's a lot. I don't personally see this happening before 8.0 though I'd be happy to be surprised. FWIW I'm a big proponent of libuv here and think we could significantly improve PHP as a language by incorporating it to solve this (and other) outstanding issues in a cross-OS manner. In the nearer term, this is exactly the sort of thing the Generator Return and Generator Delegation RFCs implemented in 7.0 are designed to handle. With a minimal amount of userland code you can resolve coroutines in userland as lightweight threads in which IO is accomplished with non-blocking libraries. The obvious drawbacks to using yield in userland as a surrogate for async/await is the proliferation of non-standard userland libraries (of which several good options already exist) to manage IO, timers and event loops. IMHO userland shouldn't know anything about *how* concurrency is achieved (whether using threads or nbio). With something like libuv internal C code would have access to a built-in worker thread pool designed specifically for a non-blocking paradigm. This would allow us to expose the best of both worlds. Just my $0.02 on the topic.

Stephen Coakley

10 years ago
On 09/29/2015 11:15 AM, Daniel Lowrey wrote:
>> On Sep 28, 2015, at 3:29 AM, S.A.N <ua.san.alex@gmail.com> wrote:> > Are there any future plans for - async/await?> This need to know now, not to use these words to constants, and class names...> > -- > PHP Internals - PHP Runtime Development Mailing List> To unsubscribe, visit: http://www.php.net/unsub.php> > > Would this be awesome? Yes. The issue is that all of PHP's built-in IO > operations are currently synchronous. It would/will require a massive > effort to (1) install the infrastructure to offload blocking operations to > worker threads and/or (2) convert many synchronous IO operations to a > non-blocking paradigm. Someone has to do that work ... and it's a lot. I > don't personally see this happening before 8.0 though I'd be happy to be > surprised. > > FWIW I'm a big proponent of libuv here and think we could significantly > improve PHP as a language by incorporating it to solve this (and other) > outstanding issues in a cross-OS manner. > > In the nearer term, this is exactly the sort of thing the Generator Return > and Generator Delegation RFCs implemented in 7.0 are designed to handle. > With a minimal amount of userland code you can resolve coroutines in > userland as lightweight threads in which IO is accomplished with > non-blocking libraries. > > The obvious drawbacks to using yield in userland as a surrogate for > async/await is the proliferation of non-standard userland libraries (of > which several good options already exist) to manage IO, timers and event > loops. > > IMHO userland shouldn't know anything about *how* concurrency is achieved > (whether using threads or nbio). With something like libuv internal C code > would have access to a built-in worker thread pool designed specifically > for a non-blocking paradigm. This would allow us to expose the best of both > worlds. > > Just my $0.02 on the topic. >
I think we can spoonfeed the massive undertaking a bit by spreading it across several iterations just to get things going. An idea I had was to just begin by providing coroutines as part of the language. That would include a `\Coroutine` class, as well as await / async keywords. This would at least enable third party async libraries to be more compatible since they would have the same await types and semantics. The next step (or at the same time as the first) is to provide some sort of `\EventLoopInterface` along with \EventLoop::getLoop(): \EventLoopInterface \EventLoop::setLoop(\EventLoopInterface) \EventLoop::run() \EventLoop::stop() \EventLoop::tick() To start out, just provide the internal coroutine scheduler as a default implementation. Coroutines would use the "event loop" to schedule resumes. Doing so would allow even more interoperability if a library chose to implement `\EventLoopInterface` in its event loop, where it handles coroutine scheduling (or lets it fall back to the internal one), and could also provide I/O "extras". The final step would be to bring libuv into PHP, itself as a better default implementation of `\EventLoopInterface`. Just my idea.
-- Stephen

Alex Sky

10 years ago
> I think we can spoonfeed the massive undertaking a bit by spreading it > across several iterations just to get things going. An idea I had was to > just begin by providing coroutines as part of the language. That would > include a `\Coroutine` class, as well as await / async keywords. This would > at least enable third party async libraries to be more compatible since they > would have the same await types and semantics. > > The next step (or at the same time as the first) is to provide some sort of > `\EventLoopInterface` along with > > \EventLoop::getLoop(): \EventLoopInterface > \EventLoop::setLoop(\EventLoopInterface) > \EventLoop::run() > \EventLoop::stop() > \EventLoop::tick() > > To start out, just provide the internal coroutine scheduler as a default > implementation. Coroutines would use the "event loop" to schedule resumes. > > Doing so would allow even more interoperability if a library chose to > implement `\EventLoopInterface` in its event loop, where it handles > coroutine scheduling (or lets it fall back to the internal one), and could > also provide I/O "extras". > > The final step would be to bring libuv into PHP, itself as a better default > implementation of `\EventLoopInterface`. >
EventLoop interface, on development stage: https://github.com/async-interop/event-loop PHP wrappers for libev and libeio supported PHP 7. https://pecl.php.net/package/ev https://pecl.php.net/package/eio libuv - certainly better because it has everything you need and a huge community. Very need async/await in the PHP core (based on generators). Perhaps there are plans in core developers, for implement async/await?

Stephen Coakley

10 years ago
On 05/04/2016 05:59 AM, S.A.N wrote:
> EventLoop interface, on development stage: > https://github.com/async-interop/event-loop
That's a userland design for event loops; async/await and coroutines don't necessarily depend on an event loop. They could be added to the language without an event loop, and simply require the user to provide the event loop. My idea was to make a transition into async as simple as possible. Also, I'm partially responsible for the event loop interface above. :P
> PHP wrappers for libev and libeio supported PHP 7. > https://pecl.php.net/package/ev > https://pecl.php.net/package/eio > > libuv - certainly better because it has everything you need and a huge > community.
I agree; libuv is probably the best of the bunch, if we include an event loop library in future versions of PHP. It is not necessary to have a PHP wrapper for libuv, since if it was included in the interpreter itself we would be just using the C interface.
> Very need async/await in the PHP core (based on generators). > Perhaps there are plans in core developers, for implement async/await?
/Legend speaks of such plans, but they come and go in whispers. Like a shadow, or a mist from the east. The prophecy spake of such features targeting PHP8; lo, most believe it to be myth./
-- Stephen

Niklas Keller

10 years ago
2016-05-04 16:40 GMT+02:00 Stephen Coakley <me@stephencoakley.com>:
> On 05/04/2016 05:59 AM, S.A.N wrote: > >> EventLoop interface, on development stage: >> https://github.com/async-interop/event-loop >> > > That's a userland design for event loops; async/await and coroutines don't > necessarily depend on an event loop. They could be added to the language > without an event loop, and simply require the user to provide the event > loop. >
Would be strange to have async / await in the language without an event loop, but it's possible, yes.
> My idea was to make a transition into async as simple as possible. > > Also, I'm partially responsible for the event loop interface above. :P > > PHP wrappers for libev and libeio supported PHP 7. >> https://pecl.php.net/package/ev >> https://pecl.php.net/package/eio >> > > > >> libuv - certainly better because it has everything you need and a huge >> community. >> > > I agree; libuv is probably the best of the bunch, if we include an event > loop library in future versions of PHP. It is not necessary to have a PHP > wrapper for libuv, since if it was included in the interpreter itself we > would be just using the C interface. > > Very need async/await in the PHP core (based on generators). >> Perhaps there are plans in core developers, for implement async/await? >> > > /Legend speaks of such plans, but they come and go in whispers. Like a > shadow, or a mist from the east. The prophecy spake of such features > targeting PHP8; lo, most believe it to be myth./
Why do we have to wait until PHP 8? Should be mostly backwards compatible and be fine in 7.2 or so. Issue is probably more deciding on an API.

Alex Sky

10 years ago
>> /Legend speaks of such plans, but they come and go in whispers. Like a >> shadow, or a mist from the east. The prophecy spake of such features >> targeting PHP8; lo, most believe it to be myth./ > > > Why do we have to wait until PHP 8? Should be mostly backwards compatible > and be fine in 7.2 or so. Issue is probably more deciding on an API. > >
Yes, I think so, too, do not need to wait for version 8, can start 7.1-2 possible even as another SAPI. P.S. These works will accelerate 5x-10x https://gnugat.github.io/2016/04/13/super-speed-sf-react-php.html

Stephen Coakley

10 years ago
On 05/04/2016 11:44 AM, Niklas Keller wrote:
> 2016-05-04 16:40 GMT+02:00 Stephen Coakley <me@stephencoakley.com>: > Why do we have to wait until PHP 8? Should be mostly backwards compatible > and be fine in 7.2 or so. Issue is probably more deciding on an API. >
I don't think we should wait, I was just thinking that it might not be ready until that time. Also, if we plan on rewriting streams and I/O to all be async and use libuv underneath, that would probably be a BC break unless the existing functions just become blocking interfaces for a separate async API. If it was a large BC break, it probably would need to wait for PHP 8. Now that I think about it, that would be our chance to replace stream resources with classes... $file = \php\stream::open("file.txt"); $bytes = await $file->read(1024); $file->close(); Then rewrite fread() and friends to be aliases that block instead of await results.
-- Stephen

Alex Sky

10 years ago
> I don't think we should wait, I was just thinking that it might not be ready > until that time. > > Also, if we plan on rewriting streams and I/O to all be async and use libuv > underneath, that would probably be a BC break unless the existing functions > just become blocking interfaces for a separate async API. If it was a large > BC break, it probably would need to wait for PHP 8. > > Now that I think about it, that would be our chance to replace stream > resources with classes... > > $file = \php\stream::open("file.txt"); > $bytes = await $file->read(1024); > $file->close(); > > Then rewrite fread() and friends to be aliases that block instead of await > results.
This is a great opportunity to normalize API standard libraries PHP :)
> $bytes = await $file->read(1024);
It is rarely necessary, more often needs $result = await $db->query($sql); and $result = await $noSql->get($key);