4

Is there a way to execute a function at regular interval?

I have a database table and I need to know when an entry is added or removed. The logic am trying to use is Ajax makes a call to Server, but instead of responding immediately, server continuously checks for 30 seconds if database is updated, if yes then only then it responds, else it responds after 30 seconds. This way I am trying to minimize the load on server by calling Ajax requests every second.

How do I do this? Does using while loop make sense ? Something like this may be-

while (SomeCondition)
{
   if (CheckIfDatabaseChanged())
   {
      echo "System Updated";
      break;
   }
}

If this is a no non-sense solution then how can I make sure that the loop runs only for 30 seconds and breaks. Or is there a better solution?

skos
  • 4,102
  • 8
  • 36
  • 59
  • You can specify time for ajax request... – Miqdad Ali Jun 22 '12 at 09:41
  • If you called that with ajax every second your server would go into a spin, as the ajax calls would never return.. and you could end up with 10000's of spinning loops added to 1 by the second while it waits, why not just ajaxcall the "CheckIfDatabasechanged" with ajax and return, if it has then do something.. – BugFinder Jun 22 '12 at 09:43
  • Suggested reading: [sleep()](http://php.net/manual/en/function.sleep.php), [set_time_limit()](http://php.net/manual/en/function.set-time-limit.php), [time()](http://php.net/manual/en/function.time.php). Your question is so trivial, that there's no better way to help you short of just writing the code for you. – lanzz Jun 22 '12 at 09:43
  • @lanzz I am not expecting anybody to write the entire code for me. But at least can I know whether its a good idea to use `while` loop in such scenario ? – skos Jun 22 '12 at 09:47
  • Any kind of loop will be ok. Other than that, see the doc links I provided, you should be able to put something together. – lanzz Jun 22 '12 at 09:48

2 Answers2

7

What you are thinking off is something called long-polling and it does not scale good on PHP especially when you use blocking IO.

See https://stackoverflow.com/a/6488569/11926 for some more information.

But your code could look something like this

set_timeout_limit(31);
$i=0;
while ($i<30) {
    // poll database for changes which is a bad idea.
    i = i + 1;
    sleep(1); // sleep 1 second
}

I bet you you can not run many of these concurrent.My advice would be to use something like redis pubsub to notify of db changes and some kind of long-polling/websocket solution instead.

If possible you should spawn a background process to subscribe to database changes and then publishes changes to pusher for example, because having multiple long running processes is really bad for performance.

You could host both of them yourself or use hosted services like for example:

Redis:

Long-polling / Websocket:

They both have small free plans which could get you started and when you get too big for these plans you could think about hosting these solutions for yourself.


P.S: I have also found a non-blocking solution in PHP which is called React. This solution might scale(better) in PHP.

Community
  • 1
  • 1
Alfred
  • 60,935
  • 33
  • 147
  • 186
0

Use this:

set_timeout_limit(0)

http://se.php.net/manual/ru/function.set-time-limit.php

Eugene
  • 1,690
  • 3
  • 16
  • 30
  • 3
    This is kind-of exactly the opposite of what question asks (how to limit execution to 30 seconds) – lanzz Jun 22 '12 at 09:44