Will php sleep/usleep strain the server?

961 Views Asked by At

I am experimenting with long polling. I use jquery ajax to call to a php script on the server.

This is my code:

function getlatest($a){

    $stmt = $dbconnect->prepare("SELECT `timestamp` FROM `test`");
    $stmt->execute();
    $stmt->bind_result($r);
    $timestamp = array();
    while($stmt->fetch())
    {
         array_push($timestamp,$r);
    }

    asort($timestamp);
    $x = end($timestamp);

    if($x > $a){
        //do this
    } else {
        sleep(5);
        getlatest($a);
    }

}

I'm trying to make a simple chat system for our office. With just about a hundred connected users running this script at the sametime for a whole day.

Will the sample code above consume a large amount of system resource?

I just can't put this online and test it because I already got a last warning from my hosting.

1

There are 1 best solutions below

4
On

You will make and endless loop with this function, PHP is server side and starts output after it finished running your script. Only then it will be sent to the users browser. why not use a setInterval in javascript and let the users browser do it?