Multithreaded PHP

This is a discussion on "Multithreaded PHP" within the PHP Forum section. This forum, and the thread "Multithreaded PHP are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack (55) Thread Tools
  55 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old Jan 4th, 2007, 02:38
Rob's Avatar
Rob Rob is offline
Head Admin & CEO

SuperMember
Join Date: Jul 2003
Location: at my desk
Age: 34
Posts: 2,952
Blog Entries: 7
Thanks: 7
Thanked 4 Times in 4 Posts
Send a message via MSN to Rob Send a message via Skype™ to Rob
Multithreaded PHP

Whilst working on a new webmaster tool (which I will unleash to you lot in the future) I ran into an issue...

The tool performs dozens of checks on a web page and then returns a report.

The problem was that the speed was very very slow indeed and I needed a way to speed it up.

What I needed was a way to handle these checks in a multithreaded way (or at least a pseudo multithreaded way) so I rang up the only person I knew who could offer a suggestion (Graham Ellis of Well House Consultants)

He suggested a workaround and whilst it isnt true multithreading, it added severe speed to my application.

Here are the two core files which make it all happen.

This first file is the main php file you hit in your browser and for the sake of this example, needs a url passed in the querystring. The url is then passed to the 6 scripts by way of a command line parameter (yes, you read that right)

Code: Select all
<?php
 
$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<count($scripts); $p++) {
    $fh[$p] = popen("/usr/local/bin/php ".
    "/home/yoursite/public_html/" . $scripts[$p] ." " .
    $url,"r");
    }
 
########################################
# DO AS MUCH PROCESSING IN HERE AS YOU LIKE
########################################
# your scripts here
# code here is processed whilst the above script calls are being processed
#
#
#
#
 
# We're done with our work - read back the results from the scripts
 
for ($p=0; $p<count($scripts); $p++) {
    $st = fgets($fh[$p]);
    $rs .=  $scripts[$p] . " : " . $st ."<br />";
    }
 
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

Code: Select all
<?
$url = $argv[1];
// code goes here
// in my webmaster tool, each of these scripts does a function
// such as check pageRank, alexa rank, backlinks, etc.
print "this is the returned value";
 ?>
The above method is best used on very intensive tasks that typically take a long time.

From my own tests, it's best to split up around 50% of the work. With that 50%, farm it out to a few different scripts and execute them with the above method. The other 50% can be processing within the main script before you finally read the results back in.

There are probabally better ways to further optimise and maybe better ways to split up the work load, but all of that experimentation I shall leave up to you.

Obviously this method can only be used in certain situations like mine... it wont make any differnce to scripts that are processor intensive, but for those which are not - this will reall add some speed if your script is suitable for splitting up in this way.

Be sure to post here with any info you gather.

Happy Coding

Digg this article
__________________
Rob - SEO Specialist
Owner & Founder of Webforumz.com

I am currently unavailable for private work

Last edited by Rob; Jan 4th, 2007 at 13:01.
Reply With Quote

  #2 (permalink)  
Old Jan 4th, 2007, 08:04
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Multithreaded PHP

Many thanks for the "plug", Rob ... the work you've done and shown me is fascinating - I'll be coming back and running it against a sampling of my pages and it should be a great help to show us how we can improve our placement.

I have used similar techniques (though through Tcl/Expect or Python) in the past to check which systems are available on a network. It was a similar case to yours in that I wanted to check up on some 20 hosts, but couldn't afford to wait for serial ping timeouts. So I "paralleled" them. To this day, I have a Tcl page on our main web sites that, in a single click, tells me about some 10 different servers on which we look after sites ... and I can check from anywhere in the world.

One caution for anyone using this technique. I believe that pipes have a 4k buffer in them, so your popen'd scripts could come to a temporary stop if they generate a lot of output and haven't been "reaped".
Reply With Quote
  #3 (permalink)  
Old Jan 4th, 2007, 09:39
Ryan Fait's Avatar
SuperMember

SuperMember
Join Date: May 2006
Location: Las Vegas
Posts: 3,786
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Multithreaded PHP

That's cool! I can seriously use that in a project I'm working on!
Reply With Quote
  #4 (permalink)  
Old Jan 4th, 2007, 09:50
Tim356's Avatar
Reputable Member
Join Date: Nov 2003
Location: Australia
Age: 25
Posts: 331
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Tim356
Re: Multithreaded PHP

If it works as you say it does, then I will also be able to use it in a current project. Thanks guys!
Reply With Quote
  #5 (permalink)  
Old Jan 31st, 2007, 18:00
New Member
Join Date: Jan 2007
Location: argentina
Age: 25
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Multithreaded PHP

hi, i also needed some multithreading/parallel task.
i had a different approach, using WebService like interface and sockets.

I have the main file, socket_cli_test.php, the one called by the user in the Browser.

That files calls internally n other ones via socket connection (HTTP request) and then grabs the answers. Ive also implemented time out, because i needed the whole process to finish before 3 seconds.

here is the code:
socket_cli_test.php
PHP: Select all

<?php
/*
 * Created on 31/01/2007
 *
 * Coded by: Ropu
 *  - Buenos Aires, Argentina  - z-tests
 */
error_reporting(E_ALL);
$cant_sockets 9;
$timeout 2.9;

// Write
$sockets = array();
$URI '/~ropu/tests/socket_srv_test.php';
$Host =  'localhost';
$port 80;

echo 
"start\n<xmp>";

list(
$start_m$start_s) = explode(' 'microtime());
$start $start_m $start_s;

// Create a new socket
for($i=0$i<$cant_sockets$i++) {
    
$sockets[$i] = socket_create(AF_INETSOCK_STREAMSOL_TCP);
}

// Connect to destination address
for( $i 0$i $cant_sockets$i++ ) {
    
socket_connect($sockets[$i], $Host$port);
//    socket_set_nonblock($sockets[$i]);
    
socket_set_option($sockets[$i], SOL_SOCKETSO_RCVTIMEO, array("sec" => 0"usec" => 500000));
}

for(
$i=0$i<$cant_sockets$i++) {
    
$ReqBody $i;
    
$ReqHeader =     "POST $URI HTTP/1.1\n".
                            
"Host: $Host\n".
                            
"User-Agent: Ropu Soft\n".
//                            "Authorization: Basic ".base64_encode($merid.':'.$merkey) . "\n".
                            
"Content-Type: application/xml\n".
                            
"Connection: close\n"// Important HEADER!!! to avoid delays when doing do{}while(reading)! 
                            
"Content-Length:  "strlen($ReqBody) ."\n\n"// without this one, post wont work
                            
"$ReqBody\n";
    
socket_write($sockets[$i], $ReqHeader);
}

$mid $start;
while(
$mid-$start $timeout){
    
$new_socks $sockets;
    
// Calculate the remaing time before timeOut
    
$working $timeout - ($mid-$start);
    
$rsec = (int)($working);
    
$rusec = (int)(($working $rsec) * 1000000);
    
    
// Watch for activity in any socket
    
$num_changed_sockets socket_select($new_socks$write NULL$except NULL$rsec $rusec);
    for(
$i=0$i<count($new_socks); $i++) {
        
// Read all info in the socket
        
do {
            echo 
$read socket_read($new_socks[$i], 2048);
        }while(!empty(
$read));
//        echo "sock: ". $i . " => " .socket_strerror(socket_last_error($new_socks[$i])) ."\n";
    
}
    
// Discard used sockets
    
$sockets array_diff($sockets$new_socks);
    
// If all sockets are read, don't wait for the timeOut.
    
if(count($sockets) < 1)
        break;
    
// Calculate actual time
    
list($mid_m$mid_s) = explode(' 'microtime());
    
$mid $mid_m $mid_s;
}

list(
$end_m$end_s) = explode(' 'microtime());
$end $end_m $end_s;
echo 
"\n\nTardó: ". ($end-$start) ." segs"

?>
socket_srv_test.php
PHP: Select all

<?php
/*
 * Created on 31/01/2007
 *
 * Coded by: Ropu
 *  - Buenos Aires, Argentina  - z-tests
 */

// set_time_limit(1);
//print_r($_POST);
//print_r($HTTP_RAW_POST_DATA); echo "\n";
$_POST['id'] = $HTTP_RAW_POST_DATA;
 
$id rand(100100000);
 
$message_log fopen('test.log'"a");
 
fwrite($message_logsprintf("\n\rIn: %s:- Id: %d %d\n",date("D M j G:i:s T Y"), $id$_POST['id'] ));
 echo 
"In: ".date("D M j G:i:s T Y") . " Id: $id " $_POST['id'] ."\n";
 
 
$delay rand(1,5);
// if($_POST['id'] == 0)
//     $delay = $_POST['id'] + 2;
//$delay = 2;
 
sleep($delay);

 
fwrite($message_logsprintf("\n\rOut: %s:- Id: %d %d Delay: %d\n",date("D M j G:i:s T Y"), $id,  $_POST['id'], $delay));
 echo 
"\nOut: ".date("D M j G:i:s T Y") ." Id: $id " $_POST['id'] ." Delay: $delay\n\n";
 echo 
"--------------------------------------------------\n\n";
 
//print_r($_SERVER);
 
?>
Feel free to send some feedback and suggestions.

hope it helps,

ropu
Reply With Quote
  #6 (permalink)  
Old Jan 31st, 2007, 21:38
Ryan Fait's Avatar
SuperMember

SuperMember
Join Date: May 2006
Location: Las Vegas
Posts: 3,786
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Multithreaded PHP

Thanks for sharing!
Reply With Quote
  #7 (permalink)  
Old May 4th, 2007, 23:08
New Member
Join Date: May 2007
Location: Asia
Age: 24
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Multithreaded PHP

Hello
can anyone help me??
I am doing the code that Rob giving but there no output it only print out the name of the file.

Is this code supported in window xp because i want to do php multithread.

What i know that php multithread and forking is not supported in window platform and if supported it can only be used in CLI am i right.

plzz someone help me i am stuck to do my project
Reply With Quote
  #8 (permalink)  
Old May 4th, 2007, 23:42
Reputable Member
Join Date: May 2006
Location: Northampton, UK
Posts: 399
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Multithreaded PHP

Grahame... i think its true to say we all love you ... :wub: :wub:
Reply With Quote
Reply

Tags
php, multithreading, multithreaded, multithread

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.webforumz.com/php-forum/12595-multithreaded-php.htm
Posted By For Type Date
Asynchronous function call? - comp.lang.php | Grupos do Google This thread Refback Jun 20th, 2008 14:59
Computer!Totaal :: Bekijk onderwerp - php thread This thread Refback May 8th, 2008 20:22
User:Csanz - ChrisDevbox This thread Refback Apr 25th, 2008 22:27
Computer!Totaal :: Bekijk onderwerp - php thread This thread Refback Apr 5th, 2008 12:30
Re: Asynchronous function call? This thread Refback Apr 3rd, 2008 13:58
Google Checkout module for Zen Cart (beta) - Page 66 - Zen Cart Support This thread Refback Mar 16th, 2008 17:38
Computer!Totaal :: Bekijk onderwerp - php thread This thread Refback Mar 6th, 2008 10:19
Asynchronous function call? - Object Mix This thread Refback Feb 29th, 2008 11:04
Re: Asynchronous function call? This thread Refback Dec 12th, 2007 11:21
Asynchronous function call? This thread Refback Nov 20th, 2007 20:29
php e processi This thread Refback Sep 3rd, 2007 06:58
kiiza's bookmarks tagged with This thread Refback Apr 25th, 2007 21:22
I’m Mike » programming This thread Refback Apr 10th, 2007 18:05
I’m Mike » Blog Archive » Fork PHP! (and speed up your scripts) This thread Pingback Apr 9th, 2007 01:12
digg / ropu / news / dugg This thread Refback Apr 9th, 2007 00:06
Digg - Who dugg or blogged: How to make a multi-threaded PHP page This thread Refback Apr 4th, 2007 14:00
Standards.za.net » PHP This thread Refback Apr 3rd, 2007 09:21
Computer!Totaal :: Bekijk onderwerp - php thread This thread Refback Mar 26th, 2007 20:38
Computer!Totaal :: Bekijk onderwerp - php thread This thread Refback Mar 6th, 2007 07:06
Computer!Totaal :: Bekijk onderwerp - php thread This thread Refback Feb 24th, 2007 10:38
Computer!Totaal :: Bekijk onderwerp - php thread This thread Refback Feb 24th, 2007 08:20
Computer!Totaal :: Bekijk onderwerp - php thread This thread Refback Feb 23rd, 2007 13:07
Computer!Totaal :: Bekijk onderwerp - php thread This thread Refback Feb 23rd, 2007 11:56
del.icio.us/url/2f62acff1b3bebe25284f85a03dbbc0a This thread Refback Feb 14th, 2007 18:04
charlvn's bookmarks tagged with This thread Refback Feb 13th, 2007 15:10
Google Checkout module for Zen Cart (beta) - Page 66 - Zen Cart Support This thread Refback Feb 13th, 2007 14:07
Digg - Multithreaded PHP with sockets thought HTTP This thread Refback Feb 1st, 2007 20:37
ropu's bookmarks on del.icio.us This thread Refback Feb 1st, 2007 18:53
Planet PHP This thread Refback Jan 10th, 2007 00:18
digg / Bara / news / dugg This thread Refback Jan 5th, 2007 07:14
digg / mindspace / news / dugg This thread Refback Jan 4th, 2007 14:54
digg / JacobHaug / news / dugg This thread Refback Jan 4th, 2007 14:54
digg / crewdesign / news / dugg This thread Refback Jan 4th, 2007 14:53
digg / spinal007 / news / dugg This thread Refback Jan 4th, 2007 14:53
digg / captainflack / news / dugg This thread Refback Jan 4th, 2007 14:53
digg / charlvn / news / dugg This thread Refback Jan 4th, 2007 14:53
digg / ForeverTangent / news / dugg This thread Refback Jan 4th, 2007 14:52
digg / jpmitchell / news / dugg This thread Refback Jan 4th, 2007 14:52
digg / t2dman / news / dugg This thread Refback Jan 4th, 2007 14:51
digg / vitalityjtw / news / dugg This thread Refback Jan 4th, 2007 14:51
digg / rob30 / news / dugg This thread Refback Jan 4th, 2007 14:51
digg / Bryan / news / dugg This thread Refback Jan 4th, 2007 14:50
digg / t2dman / news / dugg This thread Refback Jan 4th, 2007 14:49
digg / vitalityjtw / news / dugg This thread Refback Jan 4th, 2007 14:48
digg / rob30 / news / dugg This thread Refback Jan 4th, 2007 14:48
digg / ForeverTangent / news / dugg This thread Refback Jan 4th, 2007 14:48
digg / charlvn / news / dugg This thread Refback Jan 4th, 2007 14:48
digg / captainflack / news / dugg This thread Refback Jan 4th, 2007 14:47
digg / spinal007 / news / dugg This thread Refback Jan 4th, 2007 14:47
digg / crewdesign / news / dugg This thread Refback Jan 4th, 2007 14:47
digg / JacobHaug / news / dugg This thread Refback Jan 4th, 2007 14:46
digg / mindspace / news / dugg This thread Refback Jan 4th, 2007 14:46
Clug Park This thread Refback Jan 4th, 2007 11:15
Standards.za.net » » Multithreading in PHP This thread Pingback Jan 4th, 2007 10:52
digg - How to make a multi-threaded PHP page This thread Refback Jan 4th, 2007 03:10


All times are GMT. The time now is 18:45.