How can one use multi threading in PHP applications

Go To StackoverFlow.com

352

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?

2008-09-16 09:56
by Steve Obbayi
See my question and answers here: http://stackoverflow.com/questions/2101640/patterns-for-php-multi-processe - powtac 2012-05-04 22:11
...and mine here: http://stackoverflow.com/questions/209774/does-php-have-threading/14201579#1420157 - Francois Bourgeois 2013-01-08 12:01
how to use pthreads extension: http://phplobby.com/php-multi-thread-on-windows-pthreads-configuration - Emrah Mehmedov 2014-03-20 11:25
Maybe of interest: http://pthreads.org - GibboK 2016-01-06 11:09


381

Multi-threading is possible in php

Yes you can do multi-threading in PHP with pthreads

From the PHP documentation:

pthreads is an object-orientated API that provides all of the tools needed for multi-threading in PHP. PHP applications can create, read, write, execute and synchronize with Threads, Workers and Threaded objects.

Warning: The pthreads extension cannot be used in a web server environment. Threading in PHP should therefore remain to CLI-based applications only.

Simple Test

#!/usr/bin/php
<?php
class AsyncOperation extends Thread {

    public function __construct($arg) {
        $this->arg = $arg;
    }

    public function run() {
        if ($this->arg) {
            $sleep = mt_rand(1, 10);
            printf('%s: %s  -start -sleeps %d' . "\n", date("g:i:sa"), $this->arg, $sleep);
            sleep($sleep);
            printf('%s: %s  -finish' . "\n", date("g:i:sa"), $this->arg);
        }
    }
}

// Create a array
$stack = array();

//Initiate Multiple Thread
foreach ( range("A", "D") as $i ) {
    $stack[] = new AsyncOperation($i);
}

// Start The Threads
foreach ( $stack as $t ) {
    $t->start();
}

?>

First Run

12:00:06pm:     A  -start -sleeps 5
12:00:06pm:     B  -start -sleeps 3
12:00:06pm:     C  -start -sleeps 10
12:00:06pm:     D  -start -sleeps 2
12:00:08pm:     D  -finish
12:00:09pm:     B  -finish
12:00:11pm:     A  -finish
12:00:16pm:     C  -finish

Second Run

12:01:36pm:     A  -start -sleeps 6
12:01:36pm:     B  -start -sleeps 1
12:01:36pm:     C  -start -sleeps 2
12:01:36pm:     D  -start -sleeps 1
12:01:37pm:     B  -finish
12:01:37pm:     D  -finish
12:01:38pm:     C  -finish
12:01:42pm:     A  -finish

Real World Example

error_reporting(E_ALL);
class AsyncWebRequest extends Thread {
    public $url;
    public $data;

    public function __construct($url) {
        $this->url = $url;
    }

    public function run() {
        if (($url = $this->url)) {
            /*
             * If a large amount of data is being requested, you might want to
             * fsockopen and read using usleep in between reads
             */
            $this->data = file_get_contents($url);
        } else
            printf("Thread #%lu was not provided a URL\n", $this->getThreadId());
    }
}

$t = microtime(true);
$g = new AsyncWebRequest(sprintf("http://www.google.com/?q=%s", rand() * 10));
/* starting synchronization */
if ($g->start()) {
    printf("Request took %f seconds to start ", microtime(true) - $t);
    while ( $g->isRunning() ) {
        echo ".";
        usleep(100);
    }
    if ($g->join()) {
        printf(" and %f seconds to finish receiving %d bytes\n", microtime(true) - $t, strlen($g->data));
    } else
        printf(" and %f seconds to finish, request failed\n", microtime(true) - $t);
}
2013-03-19 13:55
by Baba
@Baba, I'm not able to configure and install pthreads on Xampp server. Can you help me with that - Irfan Dayan 2013-09-14 11:03
Download windows binary here http://windows.php.net/downloads/pecl/releases/pthreads/0.0.45 - Baba 2013-10-03 23:31
@Baba can you access shared SESSION variables with multi-thread - Mehdi Karamosly 2013-11-01 17:22
@danip - MPM worker for Apache has nothing to do with PHP userland threading, which is what Baba is talking about. Those are two entirely different things. MPM worker for Apache doesn't expose any threading functionality for PHP programmers, in terms of splitting units of processing across threads and then join()-ing them with main context when they're done. What MPM does is simply distribute requests in a more efficient manner to processes/threads running PHP process - N.B. 2013-12-17 12:47
That's nice, I have not touched PHP for years and now it's got multithreading capabilities - cruizer 2014-03-27 08:33
Nice and simple! Just FYI, I am deploying an app on Azure Cloud Win server and if only the basic 1 core configuration is selected the multi-threading will not be available unless more cores are added - Milan 2014-05-27 17:57
I am having trouble finding an answer to my question so I will post here...(PHP seems to be a very emotionally charged language and the level of subjective rhetoric on the subject is... well... frustrating, I digress...) The documentation mentions that it allows for user-land threads. My question is, what about within the kernel? Is PHP truly multi threaded or is it solely in userland? Documentation on the matter would be greatly appreciated as well - taylorcressy 2014-09-23 20:34
I upvoted this just because I saw that Multi-threading is possible in php - Kacy 2015-02-09 13:21
Please take a look here http://stackoverflow.com/questions/28493421/send-multiple-numbers-sms-requests-in-one-second-php I'm trying to work multithreading but i'm not sure it is working and taking more time than expected - Raheel 2015-02-13 07:02
@Baba iam using ubuntu 14.04 system. how to configure pthread in it - Abhijit Jagtap 2016-07-15 17:57
Lets say you have a php process which spawns a few threads to carry out some operations which might also involve calls to DB. Now if there is a SQL exception in 1 thread, does it crash the entire system - ankitG 2016-10-20 15:18
Why there is a weaning on not using pthreads for web and to be used only for CLI https://secure.php.net/manual/en/intro.pthreads.ph - Amr ElAdawy 2017-02-16 06:31
@Baba can i run thread by HTTP ? simple : http://yourname.com/thread.php - My Name 2017-08-19 07:58
It says that needs 7.2+ but downloads are only for 7.1.9. I'm running win32 cli php on 7.1.9, how can i use this - MacGyver 2017-09-07 23:43
Looking at this makes me want to put 2 sharpened pencils up my nose and slam my face on my desk - Alexander 2018-04-03 05:06


30

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) {
        pclose($pipe[$j]);
    }
}
2010-12-03 22:27
by masterb
I'm using the solution above, and works fine, I think that it was the most easy way to do parallel process using php - GodFather 2012-03-07 14:31
Its fork not thread - e-info128 2014-10-18 15:42
like @e-info128 said, this implementation forks the process, which means that it is running on a different process, and does not share process resources. That being said, if the job at hand does not need to share resources, then this will still work and it will run in parallel - Raffi 2017-01-10 10:23
How would you pass variables to popen without using session variables - atwellpub 2017-03-13 00:39
@atwellpub No way, these are separate processes sharing no resources. Even sessions will be awkward IPC mechanis - Cholthi Paul Ttiopic 2017-10-23 06:10
@e-info128 what's a fork - MAZux 2018-10-14 13:52


16

Threading isn't available in stock PHP, but concurrent programming is possible by using HTTP requests as asynchronous calls.

With the curl's timeout setting set to 1 and using the same session_id for the processes you want to be associated with each other, you can communicate with session variables as in my example below. With this method you can even close your browser and the concurrent process still exists on the server.

Don't forget to verify the correct session ID like this:

http://localhost/test/verifysession.php?sessionid=[the correct id]

startprocess.php

$request = "http://localhost/test/process1.php?sessionid=".$_REQUEST["PHPSESSID"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_exec($ch);
curl_close($ch);
echo $_REQUEST["PHPSESSID"];

process1.php

set_time_limit(0);

if ($_REQUEST["sessionid"])
   session_id($_REQUEST["sessionid"]);

function checkclose()
{
   global $_SESSION;
   if ($_SESSION["closesession"])
   {
       unset($_SESSION["closesession"]);
       die();
   }
}

while(!$close)
{
   session_start();
   $_SESSION["test"] = rand();
   checkclose();
   session_write_close();
   sleep(5);
}

verifysession.php

if ($_REQUEST["sessionid"])
    session_id($_REQUEST["sessionid"]);

session_start();
var_dump($_SESSION);

closeprocess.php

if ($_REQUEST["sessionid"])
    session_id($_REQUEST["sessionid"]);

session_start();
$_SESSION["closesession"] = true;
var_dump($_SESSION);
2009-07-03 14:31
by Ricardo
Last time i checked (a few years ago) php didn't allow to access file based session storage by two processes simultaneously. It locks file and second process has to sit there waiting for the first script to stop. I'm talking about webserver environment, not CLI - Alexei Tenitski 2011-05-05 21:15
Its a fork not thread - e-info128 2014-10-18 15:43
set_time_limit(0); yikes! Never, ever do this - Kafoso 2016-05-27 11:57


10

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 http://www.php.net/manual/en/ref.pcntl.php

POSIX functions http://www.php.net/manual/en/ref.posix.php

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.

2008-09-16 10:30
by J.D. Fitz.Gerald


8

I know this is an old question but for people searching, there is a PECL extension written in C that gives PHP multi-threading capability now, it's located here https://github.com/krakjoe/pthreads

2012-09-19 02:39
by JasonDavis


8

using threads is made possible by the pthreads PECL extension

http://www.php.net/manual/en/book.pthreads.php

2013-10-31 18:07
by pinkal vansia


5

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.

2010-08-26 21:33
by Pete


4

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.

2008-09-16 14:03
by Adam Hopkinson
It was a couple of years ago when i encountered this problem and eventually did the application using c++ since i also program in that language. Seeing this as an alternate solution, i am going to try it out and see how well it manages - Steve Obbayi 2008-09-17 04:32


3

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

2011-01-25 04:45
by Sheldmandu


3

I know this is way old, but you could look at http://phpthreadlib.sourceforge.net/

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

2011-02-23 15:13
by Unsigned


2

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.
2011-05-10 07:40
by Manoj Donga


2

How about pcntl_fork?

check our the manual page for examples: PHP pcntl_fork

2012-01-13 00:55
by Jarrod


2

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.

2012-02-02 04:07
by Stilero
It oughta be one reply for this - Seiji Manoan 2015-03-04 19:36


2

The Thread class is available since PECL pthreads ≥ 2.0.0.

2016-01-20 08:02
by Dhananjay Kashyap
can i run thread by HTTP ? simple : yourname.com/thread.php - My Name 2017-08-19 12:28


0

As of the writing of my current comment, I don't know about the PHP threads. I came to look for the answer here myself, but one workaround is that the PHP program that receives the request from the web server delegates the whole answer formulation to a console application that stores its output, the answer to the request, to a binary file and the PHP program that launched the console application returns that binary file byte-by-byte as the answer to the received request. The console application can be written in any programming language that runs on the server, including those that have proper threading support, including C++ programs that use OpenMP.

One unreliable, dirty, trick is to use PHP for executing a console application, "uname",

uname -a

and print the output of that console command to the HTML output to find out the exact version of the server software. Then install the exact same version of the software to a VirtualBox instance, compile/assemble whatever fully self-contained, preferably static, binaries that one wants and then upload those to the server. From that point onwards the PHP application can use those binaries in the role of the console application that has proper multi-threading. It's a dirty, unreliable, workaround to a situation, when the server administrator has not installed all needed programming language implementations to the server. The thing to watch out for is that at every request that the PHP application receives the console application(s) terminates/exit/get_killed.

As to what the hosting service administrators think of such server usage patterns, I guess it boils down to culture. In Northern Europe the service provider HAS TO DELIVER WHAT WAS ADVERTISED and if execution of console commands was allowed and uploading of non-malware files was allowed and the service provider has a right to kill any server process after a few minutes or even after 30 seconds, then the hosting service administrators lack any arguments for forming a proper complaint. In United States and Western Europe the situation/culture is very different and I believe that there's a great chance that in U.S. and/or Western Europe the hosting service provider will refuse to serve hosting service clients that use the above described trick. That's just my guess, given my personal experience with U.S. hosting services and given what I have heard from others about Western European hosting services. As of the writing of my current comment(2018_09_01) I do not know anything about the cultural norms of the Southern-European hosting service providers, Southern-European network administrators.

2018-09-01 05:04
by Martin Vahi


-2

May be I missed something but exec did not worked as asynchronous for me in windows environment i used following in windows and it worked like charm ;)

$script_exec = "c:/php/php.exe c:/path/my_ascyn_script.php";

pclose(popen("start /B ". $script_exec, "r"));
2013-10-03 05:39
by Mubashar Ahmad
To hack multithreading in PHP you should open multiple popen() statements (PHP will not wait on it to complete). Then after a half dozen or so are open you call pclose() on those (PHP will wait on the pclose() to complete). In your code you close each thread immediately after opening it so your code will not behave like multithreaded - Kmeixner 2014-06-19 17:49


-3

Multithreading means performing multiple tasks or processes simultaneously, we can achieve this in php by using following code,although there is no direct way to achieve multithreading in php but we can achieve almost same results by following way.

chdir(dirname(__FILE__));  //if you want to run this file as cron job
 for ($i = 0; $i < 2; $i += 1){
 exec("php test_1.php $i > test.txt &");
 //this will execute test_1.php and will leave this process executing in the background and will go         

 //to next iteration of the loop immediately without waiting the completion of the script in the   

 //test_1.php , $i  is passed as argument .

}

Test_1.php

$conn=mysql_connect($host,$user,$pass);
$db=mysql_select_db($db);
$i = $argv[1];  //this is the argument passed from index.php file
for($j = 0;$j<5000; $j ++)
{
mysql_query("insert  into  test   set

                id='$i',

                comment='test',

                datetime=NOW() ");

}

This will execute test_1.php two times simultaneously and both process will run in the background simultaneously ,so in this way you can achieve multithreading in php.

This guy done really good work Multithreading in php

2013-11-05 12:51
by Pir Abdul
Link at bottom no longer work - Kmeixner 2014-06-19 17:46
Also, this has nothing todo with MultiThreading. This is parallel-processing. Totally different things - Digital Human 2017-12-11 14:28
In my opinion as a workaround, an emergency hack, the idea behind the offered solution is very appropriate, but I guess different people can have flame wars about what constitutes "true multi-threading", because there is a distinction between concurrency and hardware based parallel processing, as described at: https://www.youtube.com/watch?v=cN_DpYBzKs - Martin Vahi 2018-09-01 05:33
Ads