1

I have a PHP script configured to loop endlessly using set_time_limit(0) and for(;;). The script is coded to listen on a specific directory and wait for files. When files are detected, execute some commands and keep the loop going. So far this has been very successful for my needs, but I have concerns.

Are there any performance disadvantages to creating an infinite loop in PHP? How can I configure my server so that this script never stops running? Or perhaps alerts me when it has stopped running? I'd like it to launch on startup too. I have a SUSE SLES 10 box.

for (;;) {
    // Check for empty directory
    if (isEmptyDir($finishedDir)) {         
        // If so, move on
        continue;
    } else {
       // do stuff.
    }
}
4

2 回答 2

1

这可能有效,但 PHP 并不是真正为这些类似守护进程的工作而设计的。Linux/Unix 有这样的定制工具inotify,可以保证性能更好,并且可以在更改事件发生时触发 PHP 脚本。

请参阅此 SO 问题:Monitor Directory for Changes

于 2013-03-15T19:46:57.233 回答
0

您可以考虑使用内存(例如:memcached、apc)而不是使用文件,因为访问文件比访问内存更昂贵。并考虑使用一点sleep()s 来增加时间间隔。

于 2013-03-15T19:50:31.920 回答