Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asynchronous concurrent multithreading support #14193

Open
jasonccs opened this issue May 10, 2024 · 6 comments
Open

Asynchronous concurrent multithreading support #14193

jasonccs opened this issue May 10, 2024 · 6 comments

Comments

@jasonccs
Copy link

Description

Asynchronous concurrent multithreading support. With the development of the network, it is necessary for the official to install such a module by default, which is convenient for developers to continue using PHP,

@iluuu1994
Copy link
Member

Hi @jasonccs!

PHP provides the low-level mechanisms for concurrent programming. It has both coroutines, as well as fibers, and allows for non-blocking IO. These can be used (and have been used) to build concurrent APIs. For example, have a look at amphp. Backing it into the language is questionable, because to make proper use of concurrency, the language would also need to provide a way to process requests concurrently (so that another request can execute while the other is blocked). However, this also means moving away from isolated requests, which is a huge paradigm shift.

As for parallel programming, you can use parallel. The reasoning why PHP doesn't provide these is similar: Parallel programming is generally not super useful in a web-programming context. Threads are expensive, and short-lived requests spinning up threads is generally not practical.

@jasonccs

This comment was marked as off-topic.

@iluuu1994 iluuu1994 changed the title Asynchronous concurrent multithreading support. With the development of the network, it is necessary for the official to install such a module by default, which is convenient for developers to continue using PHP, Asynchronous concurrent multithreading support May 10, 2024
@Gemorroj
Copy link
Contributor

@iluuu1994 the main problem for asyns programming in php is - the need for data serialization. we cannot use one db connection or curl connetion or other non serializable data for threads (see https://github.com/amphp/serialization/tree/1.x/src).

@iluuu1994
Copy link
Member

@Gemorroj Can't you create a separate connection for each thread? I've never worked with parallel, but as far as I understand, parallel works by communicating between threads through channels, rather than sharing memory itself (popularized by Go). The DB extension would likely need to wrap the connection in a mutex anyway, if the protocol doesn't support multiplexing.

@Gemorroj
Copy link
Contributor

@iluuu1994
I have not worked with parallel. Because its prospects are vague, see krakjoe/parallel#201 (comment) .
Although, at first glance, this is exactly what I need.
And yes, for a large project it is difficult to isolate code that can be serialized. In my case, it is a Symfony container. Here is a minimal example. https://github.com/Gemorroj/parallel-nonserializable

@danog
Copy link
Contributor

danog commented May 15, 2024

@Gemorroj You're confusing threads with fibers.

Fibers do not need serialization to work, and amphp libraries never use serialization to interact between fibers.

https://github.com/amphp/mysql, https://github.com/amphp/http-client, https://github.com/amphp/postgres, etc NEVER use serialization when interacting between concurrent fibers.

All amphp libraries do NOT use threads, because threads are not needed to do async operations with fibers.

https://github.com/amphp/parallel is the only library that uses threads through karkjoe/parallel, pthreads or other solutions automatically, and it should be used only to do CPU-bound operations (mainly just cryptography and super heavy maths); basically, in the vast majority applications you will never use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants