Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2005
    Posts
    59
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder
    Join Date
    Mar 2005
    Location
    Pakistan
    Posts
    207
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #3
    New Coder
    Join Date
    Mar 2005
    Posts
    59
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!

  • #4
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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();"

  • #5
    New Coder
    Join Date
    Mar 2005
    Posts
    59
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!

  • #6
    New Coder
    Join Date
    Mar 2005
    Posts
    59
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •