Search code examples
phpcomet

php comet: eating the CPU resources


I built a website in which I used comet with php. As long I made some research and found tutorials It seems pretty easy to implement also no special requirement on a server except it will use lot of resource. Now at the moment I got notification from hosting that my script is eating the cpu resource at 90% :( I put the script off but my question is what should be best to use now? I read about node.js but also read they would need some requirement about server? Any good and easy to implement solution in this situation?

Would that be ok with VPS or dedicated servers if let say I have 2000 users each day in this comet script?

EDITED:

/*CLOSE THE SESSION WITH USER DATA*/
session_write_close();

// set time expire unlimited
set_time_limit(0);  

$OldPostID      = isset($_REQUEST['OldPostID']) ? intval($_REQUEST['OldPostID']) : 0;

$result_set_query_1 = $MClass->MyPostings($PageOwner); 

if (mysql_num_rows($result_set_query_1))
{
    $o = 0;
    while ($r = @mysql_fetch_array($result_set_query_1))
    {
        $PID = $r['PID'];
    }
}

$NewPostID = $PID;

while ($NewPostID <= $OldPostID)    
{
    usleep(10000); // sleep 10ms to unload the CPU
    clearstatcache();

    $result_set_query_2 = $MClass->MyPostings($PageOwner);

    if (mysql_num_rows($result_set_query_2))
    {
        $o = 0;
        while ($rs = @mysql_fetch_array($result_set_query_2))
        {
            $PID = $rs['PID'];
        }
    }

    $NewPostID = $PID;
}

// return a json array
$result_set_posts   = $MClass->GetAllMyPostings( $PageOwner, $OldPostID, 0); 

}

Solution

  • Comet, per se, doesn't use a lot of resources unless instructed to- it's the script. If you were to rewrite the seemingly CPU intensive script in javascript for a Node.js server, it will most likely perform the same way.

    Getting a "push" system together usually involves setting up a cloud instance-- free from Heroku, for example-- or setting a dedicated/VPS server with access to the shell.

    And Node.Js is a newer, although relatively immature implementation of "push" technologies. In order to get node running, you need to install a node.Js server. And few hosts have an installation ready to go-- but it can be easily installed via a few simple shell commands.

    But judging from the above scenario, few users would be able to use your app concurrently, since it is already running at 90% CPU for only one user. If the users were to use the app for a short while each session, then sure, 2000 users per day is feasible, depending how how many use it at simultaneously and when. Maybe.