php exec如何计算时间?

k10*_*102 2 php exec

我正在尝试在php中为我的任务实现某种"多处理".任务是检查网络中每个设备的状态.

为此,我决定使用循环exec,它的工作原理.但我不知道它是否正常工作:

$command = "php scan_part.php $i > null &";
exec($command);
Run Code Online (Sandbox Code Playgroud)

这可以scan_part.php根据需要多次调用,但问题是:我如何计算所有scan_part.php执行所需的时间?

拜托,帮助我,我被困住了!

zne*_*eak 6

使用proc_open启动脚本代替exec.Proc_open让你等到一个进程完成proc_close,等待程序终止.

$starttime = microtime(true);
$processes = array();
// stdin, stdout, stderr- take no input, save no output
$descriptors = array(
    0 => array("file", "/dev/null", 'r'),
    1 => array("file", "/dev/null", 'w'),
    2 => array("file", "/dev/null", 'w'));

while ($your_condition)
{
    $command = "php scan_part.php $i"; // no pipe redirection, no background mark
    $processes[] = proc_open($command, $descriptors, $pipes);
}

// proc_close will block until the program terminates or will return immediately
// if the program has already terminated
foreach ($processes as $process)
    proc_close($process);
$endtime = microtime(true);

$delta = $endtime - $starttime;
Run Code Online (Sandbox Code Playgroud)