duanbipu1720 2016-09-09 16:51
浏览 102

PHP守护进程忽略睡眠功能

I wrote daemon script in php, which works using hosted server. I haven't got access to shell, so I execute it using popen function and CLI

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

Everything works fine, except it ignores 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();
}

Added links to full code preview

Full daemon code can be previewed here: http://pasted.co/ffa1d125

Code which manages (starts / stops) daemon: http://pasted.co/e3b575b0

Method iterate consists of mainly usleep(5000000) function. I ran daemon for 30 seconds, so I should have received max 6 iterations. Instead of that I received over 600.

Why it ignores the sleep fuction? Is it due to CLI execution? How to force daemon to use sleep to not kill my CPU?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 Revit2020下载问题
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大