3

I have been trying to use threads in PHP for my application but could not find a supporting DLL for Windows for PHP VC15 7.4. When I was looking for more details and tutorials on threads in PHP, I found that the PHP Documentation is pointing to use a parallel extension instead of the pthreads extension as you can see at https://www.php.net/manual/en/intro.pthreads.php

It says

Consider using parallel instead.

I could not find any code example or tutorial, nor I could really understand what this "Parallel" is, but I believe it must be something special since the PHP Documentation itself is recommending that I consider it.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Airy
  • 5,484
  • 7
  • 53
  • 78
  • 1
    Check [this GitHub repo](https://github.com/krakjoe/parallel) out. They also have given a link to [manuals in php.net](https://www.php.net/parallel)... – Romeo Sierra Mar 31 '20 at 10:53
  • Also check https://www.php.net/manual/en/philosophy.parallel.php and https://www.php.net/manual/en/class.parallel-runtime.php example down the page. – dabuno Mar 31 '20 at 10:58
  • @RomeoSierra I visited Github Repo and tried to install it but it gives error saying Unable to load dynamic library 'php_parallel.dll'. I installed exactly the same version of dll as my PHP 7.4 TS VC15 64x. What can be the problem? Is it because I am running on Nginx? – Airy Mar 31 '20 at 12:23
  • @dabuno I followed your links but getting error as you can see in reply to Romeo's Comment. What can be the problem? – Airy Mar 31 '20 at 12:24
  • I found another Parallel Library, https://github.com/amphp/parallel. Is it the same as krakjoe/parallel? Can I do any thread work with it? – Airy Mar 31 '20 at 12:31
  • @AbdulJabbarDumrai it seems parallel is not bundled with php. It's an extension you have to load. https://windows.php.net/downloads/pecl/releases/parallel/1.1.3/ - guide to install https://www.php.net/manual/en/parallel.setup.php#parallel.setup – dabuno Mar 31 '20 at 15:02
  • The PHP documentation is crowd-sourced IIRC, not written by the developers/maintainers of the PHP language. – TylerH Jan 11 '22 at 17:03
  • To that point, the page you link to *links directly to the Parallel extension documentation table of contents*... is that not enough to answer your questions? – TylerH Jan 11 '22 at 17:12

1 Answers1

8

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
behzad m salehi
  • 1,038
  • 9
  • 23