php exec如何计算时间? [英] php exec how to calculate time?

查看:114
本文介绍了php exec如何计算时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to implement some kind of 'multiprocessing' in php for my task. The task is to check the status of every device in our network.

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

For that i decided to use looped exec and it works. But i don't know whether it works fine or not:

$command = "php scan_part.php $i > null &";
exec($command);

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

This calls scan_part.php as many times as i need, but the question is: how can i calculate time needed for all of my scan_part.php's to be executed?

请帮帮我,我被卡住了!

Please, help me, i'm stuck!

推荐答案

使用 proc_open 来启动脚本,而不是exec. Proc_open 可让您等到通过<一个href ="http://ca2.php.net/manual/fr/function.proc-close.php" rel ="nofollow"> proc_close ,它一直等到程序终止. /p>

Use proc_open to launch your script instead of exec. Proc_open lets you wait until a process is done through proc_close, which waits until the termination of the program.

$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;

这篇关于php exec如何计算时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆