Search code examples
phpmysqlajaxapachelong-polling

Apache wont serve while polling


I have Wamp setup on my windows 8.1 machine which I am using for development. My problem is Apache wont serve me pages from the specific web app I am working on in a reasonable time while I am running the script that is doing the polling. Here is the script and its back end implementation

window.fetch_messages = function () // I call this when my page is loaded
{
    var last_message = $("div.message:last").attr('data-ai_id');
    var project_id = getParameterByName('project-id'); // Another one of my helpers

    $.ajax({
            url:'project_messages',
            type:'POST',
            data:{ project_id:project_id, latest_message:last_message },
            timeout:50000,
            success:new_messages, 
            error:function(data){ console.log(data); setTimeout(fetch_messages(),50000); }
    });

};

And the backend

do
{
    $messages = $this->mentor_model->query_messages($this->project_id,$this->viewer, $this->last_message_id);

    if($messages)
    break;

    usleep(25000);
}
while(empty($messages));

echo json_encode($messages);
exit;

This all works but I cant work properly if apache is not responding to my other request to go to another page or something in a reasonable time. I have other web apps on the machine and they will work fine while polling but the web app itself wont respond to other requests in reasonable time and this only happends when I'm on the page that uses this script. As a note I also made sure mysql was not giving the issues here by visiting another wapp(coining) on the localhost that uses mysql and it responds fine.

What's apache's deal ? is there some setting or something I have to change. It should be able to handle this fine since its just me testing.


Solution

  • This is more than less a resource handle problem. The all round use of sessions were being blocked because the script in question was not allowing session data use while it was running (because it was using the data).

    A simple session_write_close() placed in the loop just before calling usleep()/sleep() on the script solved my problem.

    Placing it anywhere after you have done using the session data should solve yours.