Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-26-2005, 05:35 AM   PM User | #1
ideffect
New Coder

 
Join Date: Mar 2005
Posts: 59
Thanks: 1
Thanked 0 Times in 0 Posts
ideffect is an unknown quantity at this point
How to run the same process multiple times at the same time? (Forking??)

Hello,

I have a simple script that gets data from a mysql database and runs a process for each item selected from the database using the while command. I wanted to find out if it is possible to run a section of the script multiple times at the same time with control of how many children processes it runs at once. I belive it's called forking but I am still new to php and have no idea on where to begin or if it's easily possible.

Here's my current script. It's still a work in progress but should give you an idea of what I'm trying to accomplish.

PHP Code:
<?
//Database Connection
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@
mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM su";

$result=mysql_query($query);
$num=mysql_numrows($result);

$j=0;
while (
$j $num) {
$address=mysql_result($result,$j,"address");
$email=mysql_result($result,$j,"email");
$lr=mysql_result($result,$j,"lr");

//I want this section to run multiple times at the same time

$fsock fsockopen("$address"80$errno$errstr5);
if (
$errno == "0") { 
$query "UPDATE su SET success = success+1, lr = 'up' WHERE address = '$address'";
fclose($fsock);

if (
$lr == "dn") {
$subject="Site back online";
$msg="Your Server Appears To Be Back Online.  The address $address is now reachable.";
$mailheaders="From:  Server Report";
mail ($email$subject$msg$mailheaders);
}
}

else {
$query "UPDATE su SET failed = failed+1, lr = 'dn'  WHERE address = '$address'";
if (
$lr == "up") {
$subject="Site not responding";
$msg="Your Server Appears To Be Offline.  The address $address was unreachable. You will recive a e-mail when the site is again reachable.";
$mailheaders="From:   Server Report";
mail ($email$subject$msg$mailheaders);
}
}

mysql_query($query);
//End PHP fork process?
++$j;
}

mysql_close();

?>
If you have any suggestions or a good tutorial on this subject, that would be a great help!

Thank you.

Last edited by ideffect; 04-26-2005 at 04:13 PM..
ideffect is offline   Reply With Quote
Old 04-26-2005, 11:56 AM   PM User | #2
amir
Regular Coder

 
Join Date: Mar 2005
Location: Pakistan
Posts: 207
Thanks: 0
Thanked 0 Times in 0 Posts
amir is an unknown quantity at this point
Forking

Helllo Friend!
H R U.
I hope this will help u a little bit.
PLZ c this 'pcntl_fork' in the PHP Manual.
Regards,
Aamir.
__________________
God helps those who help others.
amir is offline   Reply With Quote
Old 04-26-2005, 03:53 PM   PM User | #3
ideffect
New Coder

 
Join Date: Mar 2005
Posts: 59
Thanks: 1
Thanked 0 Times in 0 Posts
ideffect is an unknown quantity at this point
I've already looked at pcntl_fork but I am not 100% sure what to do with it. How would I go about controling them and would it work with my current code? Will the primary script execute the code with the data from the database with the while statment?

Thanks for the help!
ideffect is offline   Reply With Quote
Old 04-26-2005, 07:22 PM   PM User | #4
Velox Letum
Senior Coder

 
Join Date: Apr 2005
Location: Colorado, United States
Posts: 1,208
Thanks: 0
Thanked 0 Times in 0 Posts
Velox Letum is an unknown quantity at this point
In the PCNTL functions page I found a few examples in the user submitted notes at the bottom which might help you. Without more time to experiment and read in depth though I cannot help you further.
__________________
"$question = ( to() ) ? be() : ~be();"
Velox Letum is offline   Reply With Quote
Old 04-26-2005, 10:40 PM   PM User | #5
ideffect
New Coder

 
Join Date: Mar 2005
Posts: 59
Thanks: 1
Thanked 0 Times in 0 Posts
ideffect is an unknown quantity at this point
A few of the examples helped me a bit but I am having trouble understanding how to use the data from the database to run each process. There were no examples there for that. If anybody has some sample code of forking using a database, please post part of it.

Thanks for th help!
ideffect is offline   Reply With Quote
Old 04-27-2005, 01:08 AM   PM User | #6
ideffect
New Coder

 
Join Date: Mar 2005
Posts: 59
Thanks: 1
Thanked 0 Times in 0 Posts
ideffect is an unknown quantity at this point
I think i'm going to run multiple copies of the script at diffrent time intervals using a where statment to pick the id's 1-50 and so on. Mabyee later on when I know more about php and forking processes I will re-write the script. Thanks for all the help!

Last edited by ideffect; 04-27-2005 at 01:14 AM..
ideffect is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 11:20 PM.


Advertisement
Log in to turn off these ads.