What is the parallel extension in PHP and how do I use it? [closed]
PHP is a single-threaded application in most common installations. JavaScript is, too, which means an application will always just run one task.
Contrary to JavaScript, however, we have no concepts like promises in PHP until version 8.1, because PHP uses 1 call stack and 1 memory heap.
What do I mean when I say "in most common installations"? Consider that, for example, a request will be served by Apache HTTPD. When a PHP (HTTP) request arrives, Apache executes the script and returns the response. While the script itself cannot launch new threads, Apache happily forks whole new processes to service multiple HTTP requests simultaneously. Such behavior can be seen on other webservers, too.
So now we know that it is possible to configure web servers to implement parallel programming with PHP.
Back to the main question: How can we implement parallel programing with PHP?
in general, for achieving such an implementation, we must use one of these 3 major methods:
- multi-processing
- multi-threading
- distributed processing
To get a visual sense of the explanations, just keep in mind that a "process" here is something like the PHP Storm application (and naturally any PHP applications).
In multi-processing (first option) we have two major approachs for implementations:
- fork
- exec
For forking an application you must use extensions like pcntl
, in this approach we actually create a new process (that has its parent), just like opening a new PHP Storm project in a separate tab. here is a complete tutorial for forking PHP applications using pcntl extension.
In executing an application, we run a file in process, and we will keep the result to give it to the parent. it uses a simple method exec()
and don't require a very complicated tutorial at all, but if you want to read more about it and see some examples you can visit this link.
The only remaining point is that multiprocessing is NOT efficient at all.
In multi-threading (second option):
You can imagine threads just like a bag of some instructions that are created inside a process.
For using the multi-threading approach, you must install ZTS PHP, which is an extension pthreads. with Zend Thread Safe (ZTS) for PHP 7.4. PHP Agent integrates with the ZTS mode, which helps to instrument multithreaded applications on PHP.
Pthreads was the keyword in your question and what you were seeking for an operational example. You can find that here and here.
Note that, in this approach, the whole process will fail if one thread fails.
In distributed programming (the last option), this can be done by:
-
socet programming
-
zeromq / gearman library
Zeromq or Gearman have some workers that do the main chore for achieving this functionality.
If you just need to implement something like promises in JavaScript, you have some additional options:
- in PHP 8.1 and above: use fibers and read this documentation
- in PHP < 8.1: use Reactphp with this git repository
As I said at the beginning, PHP uses 1 call stack and has one memory heap, but these libraries have their low-level lib for running codes async; they have an event loop in their core.