5

Possible Duplicate:
How can I stop PHP sleep() affecting my whole PHP code?
PHP: Output data before and after sleep()?

I have a php code in localhost which echo's data and sleeps in between. On the client side, I am able to see the data received in timely manner and I also render it.

$condition = true;
while($condition) {
  $data = get_some_data(); // may not fetch new data to be sent, each iteration. 
  echo $data;
  ob_flush();

  $i++;
  sleep(1);

  $condition = run_some_logic(); //could become false depending on DB value
}

But, when I open another browser and try to open another page (localhost again) , it is not even opening. When I close the first window and try to open the second URL after some time, it is working.

I think clearly this has to do with sleep. Is there a way to call sleep() or output data in intervals, without blocking all other requests?

What I am trying to do here is, not let the loop finish until a condition is satisfied.

Community
  • 1
  • 1
  • May I ask what you are trying to achieve? E.g. the use case so we can find a better solution for you – PeeHaa Aug 26 '12 at 12:41
  • What is the condition you are waiting on? – PeeHaa Aug 26 '12 at 12:42
  • You do realise the client will be waiting for your loop ? – Samson Aug 26 '12 at 12:42
  • @PeeHaa I am querying database and checking for some value. I am modifying question a bit more.. –  Aug 26 '12 at 12:43
  • @radashk Not sure exactly what you mean, but since I am using ob_flush, I get the partial data on client side and I render it - which is what I want to do. Except, I get problems when ANOTHER client logs in. –  Aug 26 '12 at 12:46
  • @RC Hmm, I don't think so. In that question, user wants to know how to avoid situation where browser renders all output data only at the end. I have already solved that. My question is regarding sleeping in non blocking way.. –  Aug 26 '12 at 12:48
  • @SenthilKumar I think the accepted answer last part is exactly what you need (ie. Ajax polling) –  Aug 26 '12 at 12:49
  • Dup http://stackoverflow.com/questions/1617412/how-can-i-stop-php-sleep-affecting-my-whole-php-code – Haru Aug 26 '12 at 12:52
  • @SenthilKumar I meant you are blocking the server-side – Samson Aug 26 '12 at 12:54
  • You're doing it wrong. Don't push periodical updates from the server, pull them from the client (with XHR). – DanMan Aug 26 '12 at 13:22

1 Answers1

4

By the looks of it simply waiting for the PHP script to end (meeting some condition based on a database change) I wouldn't do it this way, but rather use XHR requests (or AJAX as the cool kids like to call it) from the client every now and then.

This prevent the user from waiting until the state changes (unblocking) and prevent the blocking of PHP.

Simply let a AJAX run every e.g. 5 seconds to check the state with the backend to see if it has been changed.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • I am aware of AJAX polling. I was just thinking whether continuous updates can be sent by this way - not finishing the request till no more update is there. Also, in my case, updates may be just 1 or 2 characters most of the time, I didn't want to send an entire HTTP request just to fetch them. –  Aug 26 '12 at 12:53
  • 2
    It's just not possible to make this work because PHP does not multithread and you'll always halt other attempts to load the page until that sleep gets finished. You would have to go with an AJAX solution. – Haru Aug 26 '12 at 12:59
  • @350_Design - so the concept of 'waiting / looping at server side, till an updated response is available to be sent back' will not work in PHP, because sleep() will block other requests? So is the only way to get pseudo real-time data is short-polling? –  Aug 26 '12 at 15:14
  • 1
    Yeah you should do it on the client side. – Haru Aug 26 '12 at 19:44