-1

I wrote a very simple queue using redis, but I think my implementation is bad, so I want to ask for a better solution. Below is a simple flow chart of my problem.

  1. some program push data into a redis list
  2. a php program loop forever, check if the list if empty, if not empty then pop the nodes in the list into MySQL.

Step 1 is fine, but I have used while (true) in step 2, which can cause a lot of CPU waste. Any good way that can do pop operation when the list is not empty?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Joey
  • 1,233
  • 3
  • 11
  • 18

3 Answers3

1

Do a cron job instead a infinite loop for the step 2

pabgaran
  • 773
  • 4
  • 15
0

set cron job to periodically check list and pop values into your MySql tables. I read one of your comments that said you did not prefer cron jobs, so consider perhaps what I term a 'poor mans cron' which is a misnomer, because the only cost of a cron job is, well server access...

Just set a time value and a threshold. if(now() - timeValue > threshold){ do your pop to MySql thing...

it is a rare case that you want infinite loops in php, imho. here is some discussion: When are infinite loops are useful in PHP?

Community
  • 1
  • 1
NappingRabbit
  • 1,888
  • 1
  • 13
  • 18
  • I hate cron because it's hard to maintain a lot of cron jobs, I prefer doing the job by program not system, like using laravel's schedule. And your threshold is similar to sleep(); – Joey May 14 '15 at 01:46
  • no no no, the major difference is that if the threshold is not met, then the conditional logic isnt executed. The check to see if a particular time has passed is not the same as have PHP -completely stop processing- for a period of time. sleep can cause a hang. Perhaps in the case of 10 seconds, sleep may be ok. I would still avoid. – NappingRabbit May 14 '15 at 14:05
-1

the easiest solution:

while (true) {
 if ($res = fetch(data)) {
  //do your staff
 } else {
  sleep(10);
 }
}

But long running PHP script may cause memory leaks, so the best way might be cronjob, with script like this:

//do not forget to check if process is already running, not to run duplicate processes.

you can try this solution: Checking if process still running?

while ($res = fetch(data)) {
 //do your staff
}
Community
  • 1
  • 1
Andriy
  • 973
  • 8
  • 13
  • I hate crons, so I'll try using another program to check if this php program is running. – Joey May 07 '15 at 10:43