PHP守护进程忽略睡眠功能

时间:2016-09-09 16:51:20

标签: php command-line-interface sleep daemon

我在php中编写了守护程序脚本,它使用托管服务器。我无法访问shell,因此我使用popen函数和CLI

执行它

pclose(popen('/usr/bin/php full_path/index.php daemon start|exit &', 'r'));

一切正常,但忽略usleep(5000000)

while (!$this->_sigterm) {
    pcntl_signal_dispatch();

    // Save the total running children
    $processes_running = count($this->_pids);

    // See if we are within our defined child limits.
    if($processes_running >= $this->_config['max']) {
        // Now lets pause.
        $this->iterate();
        continue; // Restart.
    }

    // Lets get the next task
    if(($task = Tasks::getNextTask()) !== false) {
        // Fork process to execute task
        $pid = pcntl_fork();
        if ($pid == -1) {
            exit(1);
        } elseif ($pid) {
            $this->_pids[$pid] = time();
        } else // We are child so lets do it!
        {
            if (posix_setsid() == -1) {
                 exit(1);
            }

                // Child - Execute task
                exit(0);
        }
    }
    // Now lets pause.
    $this->iterate();
}

添加了完整代码预览的链接

可以在此处预览完整守护程序代码:http://pasted.co/ffa1d125

管理(启动/停止)守护程序的代码:http://pasted.co/e3b575b0

方法iterate主要由usleep(5000000)函数组成。我跑守护进程30秒,所以我应该收到最多6次迭代。而不是我收到超过600。

为什么它会忽略睡眠功能?是由于CLI执行?如何强制守护进程使用sleep来不杀死我的CPU?

0 个答案:

没有答案