php / php-src

The PHP Interpreter
https://www.php.net
Other
37.59k stars 7.7k forks source link

Request for Native Asynchronous Support in PHP: Introducing async and await Keywords #13095

Closed Demilade362 closed 6 months ago

Demilade362 commented 6 months ago

Description

Dear PHP Team,

I'm writing to express my enthusiasm for enhancing PHP's capabilities with native asynchronous support. The introduction of async and await keywords would significantly benefit developers, enabling streamlined asynchronous programming, concurrent execution, and non-blocking I/O operations within PHP applications.

By incorporating these features into the core language, PHP can align with modern development practices, empowering developers to create more responsive and efficient applications.

Thank you for considering this request. I believe that embracing native asynchronous support will further elevate PHP's position as a versatile and contemporary programming language.

Best regards, Oluwademilade Abatan

iluuu1994 commented 6 months ago

PHP already has coroutines and fibers, both of which can be used for non-blocking IO. What PHP doesn't have is a native event loop or IO functions that actually make use of those constructs. That's probably what you're actually looking for. You can find libraries/extensions that do that for you, like AMP or Swoole.

async/await and coroutines are quite similar in that they can pause and resume function execution at fixed program points while sending values in and out of the function. Generators have other use-cases, like iterators. For asynchronous programming specifically, PHP opted for fibers over async/await because they avoid function coloring. It's very unlikely that introducing yet another concept is helpful.

I think the main way forward is to introduce an event loop and asynchronous functions in core. There's some overlap with https://externals.io/message/122027 in this regard. For asynchronous programming to be actually useful, PHP should be able to continue execution of other requests while waiting for IO operations. Otherwise you're essentially just blocking in the event loop instead of in the blocking IO call, unless you're using something like a joined await. AFAIK, asynchronous libraries work around this by implementing their own web servers.

This is quite a big shift in terms of PHPs philosophy, so I expect this to take some time.

Anyway, GitHub is not the place to have this discussion. Please write an e-mail to the internals mailing list if you like. Make sure to check existing topics.

https://www.php.net/mailing-lists.php https://externals.io/

Demilade362 commented 6 months ago

@iluuu1994 Thank you for taking your time to explain.