Search code examples
phpmysqlcakephpcomet

cakephp comet usleep blocks everything


Below is the code that i am end up with using successful comet implementation.

$lastmodif = isset($this->params['form']['timestamp']) ? $this->params['form']['timestamp'] : 0;
$currentmodif = $already_updated[0]['Update']['lastmodified'];

while ($currentmodif <= $lastmodif)
{
    usleep(5000000);
    clearstatcache();
    $already_updated_new = $this->Update->find('all',array
    (
        'conditions' => array
        ( 
            'Update.receiver_id' =>  $this->Auth->user('id'),
            'Update.table_name' =>  "request_responses"
        )
    ));
    $currentmodif = $already_updated_new[0]['Update']['lastmodified'];
}

$already_updated[0]['Update']['lastmodified'] is the query result for get last updated timestamp of table.

In above code $lastmodif and $currentmodif is the timestamp that is being passed after every successful comet response.

But now problem is that when i am clicking on other links on same page nothing happens but after wait for so long its redirecting.

i think usleep is blocking other HTTP request.

i am using mysql and cakephp please guys guide me what should i do in order to solve this issue.

I have tried to flush when page is called but it shows can not modify header error as output is already sent.

Thanks.


Solution

  • I've met similar situation several times. It looks like Session is blocked by your sleeping script.

    How to solve it in CakePHP:
    call session_write_close(); at the start of your script.
    There is no way to do that via Cake's Session Component or Helper
    Note: If something inside script uses session - Cake will reopen session and hang all requests that use same session again. In this case you will need to close session before sleep or before any operations that take a lot of time to be finished