1

I was implementing the server-push content using comet to the client browser. There should be updates when mysql got insert then something will happen to client, so I'm currently doing by PHP like this:

PHP
  while (check database if update is there)
  {
    usleep(10000); 
   // do write json
  }

It actually working, but what happening to me is my hosting(shared) run out of resource even when I just test with 5 simultaneous clients. CPU is 40% loading at this moment and cause account disable.

So please someone advise any idea how to trigger php to send out the new update only without looping check.

Thanaporn
  • 189
  • 2
  • 3
  • 11

3 Answers3

2

I don't know how your Javascript looks like, but I think you're approaching it incorrect. What you really wanna do is probably to send requests from the browser with 10 secs intervals. See this example.

Community
  • 1
  • 1
Andreas Bergström
  • 13,891
  • 5
  • 59
  • 53
2

I think your problem is that you have a slight error regarding usleep. The parameter should be:

micro seconds
Halt time in micro seconds. A micro second is one millionth of a second.

Your script currently halts for 0.01 seconds instead of 10 seconds, which, I assume, is not what you wanted.


use:

usleep(10 * 1000000);

or:

sleep(10);
Yoshi
  • 54,081
  • 14
  • 89
  • 103
1

This method will not use any CPU during the sleep time:

set_time_limit(0);
while (check database if update is there) {
   sleep(10); 
   // do write json
}
Matt S
  • 14,976
  • 6
  • 57
  • 76