Asynchronous PHP??? OK..maybe misleading, but need HELP speeding up a script!

Started on the old forum in June 2011 · view the original in the archive · old links redirect here

No replies · 317 views · started 15 years ago · 2 min read

Shibbey Newbie · 3 posts #1

I know PHP is, by nature, not asychronous.....so here is my situation: I currently have a CRON job that I want to run daily. It is a PHP script that cURLs multiple pages based on db entries. After adding a few lines to figure out each entry's time to complete, I figure it will take over 1.5 days for the script to finish each time it runs...and that will only get longer.

I am trying to figure out a way to speed this up significantly... I was looking at pcntl_fork(), but I am not sure how/if I can implement it for my script. I am also not sure if it would speed it up enough to matter.

I am looking for any ideas on how I can speed this up and complete the job in MUCH less time. My webhost does not seem support PCNTL, so forking does not appear to be an option. I do know my script does work, it is just a matter of speeding it up. I will even incorporate several methods if they are compatable. Forking the process for each ID looks like it would help some, but I am not positive.

So here is the basic concept behind my script. There is no information printed out on a webpage. Some code omited to simply the view...concept of script intact.

$Email = new Email;
$Characters = new Characters;
$dbUtilities = new dbUtil;
$mycURL = new mycURL;

$db = $dbUtilities->dbConnect();  //Connects to the Database
$CharacterIDList = $Characters->Get_Character_List($db, $Email);  //Gets a list of all the CharacterIDs in the Character_Info Table

foreach ($CharacterIDList as $CharacterID){
        //cURL's character webpage and stores in $info variable
        $info = $mycURL->getWebPage('http://www.xxxxxxxxxxxxxx=Chr'.$CharacterID);

        //Scrape and update Character_Info table
        $Characters->Scrape_Character_Info($CharacterID, $info, $dbUtilities, $db, $Email);

        //Scrape and Update Character Name if Changed
        $Characters->Scrape_Character_Name($CharacterID, $info, $dbUtilities, $db, $Email);

        //Scrape and Update Character friends
        $Characters->Scrape_Character_Friends($CharacterID, $info, $dbUtilities, $db, $Email);

        //Scrape and Update Character enemies
        $Characters->Scrape_Character_Enemy_Players($CharacterID, $info, $dbUtilities, $db, $Email);
        $Characters->Scrape_Character_Enemy_Kingdoms($CharacterID, $info, $dbUtilities, $db, $Email);

        //Scrape and update Character City if changed
        $Characters->Scrape_City($CharacterID, $info, $dbUtilities, $db, $Email);
}

As far as forking is concerned...I have been testing my script by loading it in a webpage and watching it go with a temporary echo after each foreach loop. I tried a simple script for forking just to see what happened. I got the fatal error. I do not know if this would change if it was executed as a CRON job or not. The test I ran was the following:

if (! function_exists('pcntl_fork')) die('PCNTL functions not available on this PHP installation');
for ($i = 1; $i <= 5; ++$i) {
    $pid = pcntl_fork();

    switch($pid) {
        case -1:
            print "Could not fork!\n";
            exit;
        case 0:
            sleep(1);
            print "In child $i\n";
            exit;
        default:
            print "In parent!\n";
    }
}

phpinfo() revealed that I have PHP version 5.2.14 installed. Beyond that, I am a noob with CRON and have never messed with PCNTL.

Can anyone offer/help this poor soul?

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close