How can one use multi threading in PHP applications

0 votes
asked Sep 16, 2008 by steve-obbayi

Is there a realistic way of implementing a multi-threaded model in PHP whether truly, or just simulating it. Some time back it was suggested that you could force the operating system to load another instance of the PHP executable and handle other simultaneous processes.

The problem with this is that when the PHP code finished executing the PHP instance remains in memory because there is no way to kill it from within PHP. So if you are simulating several threads you can imagine whats going to happen. So I am still looking for a way multi-threading can be done or simulated effectively from within PHP. Any ideas?

15 Answers

0 votes
answered by pinkal-vansia

using threads is made possible by the pthreads PECL extension

0 votes
answered Sep 16, 2008 by j-d-fitz-gerald

While you can't thread, you do have some degree of process control in php. The two function sets that are useful here are:

Process control functions

POSIX functions

You could fork your process with pcntl_fork - returning the PID of the child. Then you can use posix_kill to despose of that PID.

That said, if you kill a parent process a signal should be sent to the child process telling it to die. If php itself isn't recognising this you could register a function to manage it and do a clean exit using pcntl_signal.

0 votes
answered Sep 16, 2008 by adam-hopkinson

You can use exec() to run a command line script (such as command line php), and if you pipe the output to a file then your script won't wait for the command to finish.

I can't quite remember the php CLI syntax, but you'd want something like:

exec("/path/to/php -f '/path/to/file.php' | '/path/to/output.txt'");

I think quite a few shared hosting servers have exec() disabled by default for security reasons, but might be worth a try.

0 votes
answered Sep 3, 2010 by masterb

why don't you use popen?

for ($i=0; $i<10; $i++) {
    // open ten processes
    for ($j=0; $j<10; $j++) {
        $pipe[$j] = popen('script2.php', 'w');

    // wait for them to finish
    for ($j=0; $j<10; ++$j) {
0 votes
answered Sep 26, 2010 by pete

You could simulate threading. PHP can run background processes via popen (or proc_open). Those processes can be communicated with via stdin and stdout. Of course those processes can themselves be a php program. That is probably as close as you'll get.

0 votes
answered Sep 10, 2011 by manoj-donga

You can have option of:

  1. multi_curl
  2. One can use system command for the same
  3. Ideal scenario is, create a threading function in C language and compile/configure in PHP. Now that function will be the function of PHP.
0 votes
answered Sep 23, 2011 by unsigned

I know this is way old, but you could look at

It supports bi-directional inter-thread communication and also has builtin protections for killing off child threads (preventing orphans).

0 votes
answered Sep 25, 2011 by sheldmandu

Depending on what you're trying to do you could also use curl_multi to achieve it.

0 votes
answered Sep 2, 2012 by stilero

pcntl_fork won't work in a web server environment if it has safe mode turned on. In this case, it will only work in the CLI version of PHP.

0 votes
answered Sep 13, 2012 by jarrod

How about pcntl_fork?

check our the manual page for examples: PHP pcntl_fork

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter