Php multithread [duplicate] Php multithread [duplicate] multithreading multithreading

Php multithread [duplicate]


There are a few solutions, varying from "Hmmm, just about OK" through to "Poke out your eyes".

  1. Write your multithreaded code as a PHP extension. Probably the most supported, but you need to write in C (or another language supported for extensions).
  2. Spawn child processes onto the underlying OS, and read/write to their input/output with standard file handles. See popen for one route in, or PCNTL. Reasonable, and you can use PHP from the command line.
  3. Make other HTTP requests to yourself via CURL or similar, thus piggybacking on your web servers multi-processing capacity. Keeps all your code "web like", but runs the risk of irate support developers tracking you down and breaking thumbs.


Since this is a top result on google, php has a new extension, pthreads: http://www.php.net/manual/en/book.pthreads.php specifically for this.


Check the PCNTL library. It may help you to emulate some thread behavior.

Also there's this class:

"This class can emulate the execution of program threads using separate HTTP requests to the same script.

It establishes an HTTP connection to the same Web server to execute the same PHP script. It sends a request passing the name a function to execute and an argument to be passed to that function.

The requested script executes some code that detects the thread execution request and calls the specified function.

When the thread request script ends, the return values of the called function is returned as a serialized string.

The calling script can execute other tasks while the thread script runs. The results may be collected later when the thread script ends."