Logo Questions Linux Laravel Mysql Ubuntu Git Menu

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)
    $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.


like image 510
Dipesh Parmar Avatar asked Dec 18 '12 13:12

Dipesh Parmar

2 Answers

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

like image 127
Swayok Avatar answered Nov 16 '22 13:11


If your script uses sessions then you could notice such behavior. PHP locks the session file until the script completes.

This means that once a script starts a session, any other script that attempts to start a session using same session id is blocked until the previous script releases the lock (or terminates).

The workaround for this is to unlock the session before any lengthy process:

  • call session_start()
  • read/write any session variables
  • call session_write_close()
  • do lengthy processing
like image 1
Salman A Avatar answered Nov 16 '22 13:11

Salman A