Parallel Processing in PHP - How to Do It

Parallel processing in PHP - How do you do it?

i use exec(). Its easy and clean. You basically need to build a thread manager, and thread scripts, that will do what you need.

I dont like fsockopen() because it will open a server connection, that will build up and may hit the apache's connection limit

I dont like curl functions for the same reason

I dont like pnctl because it needs the pnctl extension available, and you have to keep track of parent/child relations.

never played with gearman...

Multithreading/Parallel Processing in PHP

It is generally not a good idea to fork an Apache process. That can cause undetermined results. Instead, using some kind of queuing mechanism is preferable. Gearman is an open source queuing mechanism you can use. I also have a blog post on the Zend Server Job Queue that talks about running tasks asynchronously Do you queue? Introduction to the Zend Server Job Queue.

You could also use something like the Zend Framework Queuing classes to implement some of the asynchronous work. Zend_Queue

@Swisstack, also I will disagree with your assertion that PHP is not created for high performance. Very seldom are language features the cause of slow performance. Perhaps by doing a raw language test comparing $a++ among different languages you will see that, but that type of testing is irrelevant. I've done consulting on PHP for several years and I have never seen a performance problem that was due to the language.

What is the parallel extension in PHP and how do I use it?

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:

  1. fork
  2. 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.



Related Topics



Leave a reply



Submit