I was enhancing some inherited PHP code which used the so-called "Daemon Loop" design pattern to read jobs off a queue and hand them off for processing by a configurable number of child worker processes. Specifically, I was trying to get the daemon to wake up once every minute (instead of every 5s) to see it there were any jobs on the queue, so in the loop (in practice the value was read from a config file into a class-level variable), I had:
sleep(60)
We were also capturing SIGCHLD for notification of when the child closed (in this instance after just a few seconds).
The problem was that the daemon was not waiting 60s to poll unless there was nothing to do. Turns out that SIGCHLD cancels the running sleep(60).
My inelegant solution? sleep(1) 60 times and then only 1s of sleep is lost!
while ($sec>0) {
sleep(1);
$sec--;
}
Thanks to Stuporglue, on whose PHP daemon example code I built a testbed to find a solution.
No comments:
Post a Comment