Multithreading in PHP

Tuesday, May 12, 2009 1:25
Posted in category PHP, Tutorial

I hear different people saying different things about multi-threading in PHP. Some say it cannot be done, no way and others say yes, it can be done. But I say, it cannot be done purely but we can implement pseudo multithreading. That is we can fake multi threading in PHP.

So how do we do it? This can be implemented with the help of 2 kinds of files. 1) the main file and other 2) the thread file. There can be any number of thread file. The main file calls the thread files and it looks like it is multi-threaded.

Let us look at a code fragment below.

 
$url = $_GET['url'];
$scripts = array(
   “script1.php”,
    “script2.php”,
    “script3.php”,
    “script4.php”,
    “script5.php”,
    “script6.php”);
 
# Set off parallel contacts to each of the scripts
 
for ($p=0; $p
    $fh[$p] = popen(“/usr/bin/php “.
    “/home/yoursite/public_html/” . $scripts[$p] .” ” .
    $url,”r”);
    }
 
/*
*    Do your processing here. The above statements will return the control to this block
*
*    Still Processing other works
*
*    Enough Processing. Now Gather the results
*/
 
for ($p=0; $p
    $st = fgets($fh[$p]);
    $rs .=  $scripts[$p] . ” : ” . $st .”
“;
    }
 
print $rs;
?>

What the above does is fire off the 6 scripts in parallel, passing $url to each script as a command line parameter. It executes each script by executing the php binary itself, so the processes themselves live outside of apache. The instant the scripts are fired, they work in the background whilst control is passed back to PHP to continue.  After we’ve done other intensive processing (whatever code you insert above) we can finally get all the scripts return values. For this example, here is the code for one of the scripts that is called – note how it reads the url we passed from the command line.

$url = $argv[1];
print “this is the returned value”;
 ?>

The above method is best used on very intensive tasks that typically take a long time. This method can increase the performance upto 50% (My personal experience and tests).

That is all. In this way we can multithread a PHP script. To be more specifi pseudo multi thread.  ;)

This is the simplest of all. I will write a tutorial on how to multithread php scripts using CURL. Till then Happy reading! ;)

VN:F [1.8.3_1051]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.3_1051]
Rating: 0 (from 0 votes)
http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/digg_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/reddit_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/stumbleupon_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/delicious_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/blogmarks_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/google_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/myspace_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/facebook_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/yahoobuzz_32.png http://www.sanjeevshrestha.com.np/wp-content/plugins/sociofluid/images/twitter_32.png
You can leave a response, or trackback from your own site.

One Response to “Multithreading in PHP”

  1. Ali Damji says:

    November 4th, 2009 at 12:42 am

    This was just what i was looking for.. simple and useful script.. Thanks..

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)

Leave a Reply