php - 一次运行多个 exec 命令(但要等待最后一个完成)

标签 php multithreading zend-framework exec

我四处寻找这个,但我似乎找不到任何人正在努力做我自己的事。

我有通过 _POST 请求传递到我的函数的信息。基于该数据,我运行 exec 命令来运行 TCL 脚本一定次数(使用不同的参数,基于 post 变量)。现在,我在 foreach 中有 exec,所以这需要永远运行(TCL 脚本需要 15 秒左右的时间才能返回,所以如果我需要运行它 100 次,我就会遇到一些问题)。这是我的代码:

    public function executeAction(){
    //code to parse the _POST variable into an array called devices

    foreach($devices as $devID){
        exec("../path/to/script.tcl -parameter1 ".$device['param1']." -parameter2 ".$device['param2'], $execout[$devID]);
    }
    print_r($execout);
}

显然,这段代码只是删除了大块内容的摘录,但希望它足以展示我正在尝试做的事情。

我需要一次运行所有的执行程序,我需要等待它们全部完成后再返回。我还需要存储在名为 $execout 的数组中的所有脚本的输出。

有什么想法吗?

谢谢!!!

最佳答案

如果您将exec() 调用放在单独的脚本中,您可以使用curl_multi_exec() 多次并行调用该外部脚本。 .这样,您就可以在单独的请求中进行所有调用,这样它们就可以同时执行。轮询 &$still_running 以查看所有请求何时完成,之后您可以收集每个请求的结果。

更新:这里是 a blog post详细说明我正在描述的内容。


例子

根据上面链接的博客文章,我整理了以下示例。

并行运行的脚本:

// waitAndDate.php

<?php
sleep((int)$_GET['time']);
printf('%d secs; %s', $_GET['time'], shell_exec('date'));

并行调用脚本:

// multiExec.php

<?php
$start = microtime(true);

$mh = curl_multi_init();
$handles = array();

// create several requests
for ($i = 0; $i < 5; $i++) {
    $ch = curl_init();

    $rand = rand(5,25); // just making up data to pass to script
    curl_setopt($ch, CURLOPT_URL, "http://domain/waitAndDate.php?time=$rand");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    curl_multi_add_handle($mh, $ch);
    $handles[] = $ch;
}

// execute requests and poll periodically until all have completed
$isRunning = null;
do {
    curl_multi_exec($mh, $isRunning);
    usleep(250000);
} while ($isRunning > 0);

// fetch output of each request
$outputs = array();
for ($i = 0; $i < count($handles); $i++) {
    $outputs[$i] = trim(curl_multi_getcontent($handles[$i]));
    curl_multi_remove_handle($mh, $handles[$i]);
}

curl_multi_close($mh);

print_r($outputs);
printf("Elapsed time: %.2f seconds\n", microtime(true) - $start);

这是我运行几次后收到的一些输出:

Array
(
    [0] => 8 secs; Mon Apr  2 19:01:33 UTC 2012
    [1] => 8 secs; Mon Apr  2 19:01:33 UTC 2012
    [2] => 18 secs; Mon Apr  2 19:01:43 UTC 2012
    [3] => 11 secs; Mon Apr  2 19:01:36 UTC 2012
    [4] => 8 secs; Mon Apr  2 19:01:33 UTC 2012
)
Elapsed time: 18.36 seconds

Array
(
    [0] => 22 secs; Mon Apr  2 19:02:33 UTC 2012
    [1] => 9 secs; Mon Apr  2 19:02:20 UTC 2012
    [2] => 8 secs; Mon Apr  2 19:02:19 UTC 2012
    [3] => 11 secs; Mon Apr  2 19:02:22 UTC 2012
    [4] => 7 secs; Mon Apr  2 19:02:18 UTC 2012
)
Elapsed time: 22.37 seconds

Array
(
    [0] => 5 secs; Mon Apr  2 19:02:40 UTC 2012
    [1] => 18 secs; Mon Apr  2 19:02:53 UTC 2012
    [2] => 7 secs; Mon Apr  2 19:02:42 UTC 2012
    [3] => 9 secs; Mon Apr  2 19:02:44 UTC 2012
    [4] => 9 secs; Mon Apr  2 19:02:44 UTC 2012
)
Elapsed time: 18.35 seconds

希望对您有所帮助!

附带说明:确保您的 Web 服务器可以处理这么多并行请求。如果它按顺序为他们服务,或者只能同时为很少的人服务,那么这种方法对你几乎没有任何好处。 :-)

关于php - 一次运行多个 exec 命令(但要等待最后一个完成),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9978964/

相关文章:

php - 从 PHP 向 MySQL 分配 NULL 时遇到问题

java - 在 javafx 中一起处理鼠标移动和单击

java - StringBuffer 追加 ("")

internet-explorer - Zend Framework - Internet Explorer - phpsessid cookie 问题

php - 'Zend_Exception' -> 'Registry is already initialized'

php - PHP 中 !empty($variable) 和 count($variable) 的 bool 结果总是相等吗?

php - 字符串上的正则表达式否定回顾

php - file_get_contents() 是阻塞函数吗?

java - 为什么局部变量不允许使用 volatile 关键字?

php - 使用 FreeTDS 库通过 Zend Framework 连接 MSSQL Server