Kev*_*vin 11 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);
}
Run Code Online (Sandbox Code Playgroud)
显然,这段代码只是删除了大块的摘录,但希望它足以证明我正在尝试做的事情.
我需要立即运行所有的执行程序,我需要等待它们全部完成才能返回.我还需要存储在名为$ execout的数组中的所有脚本的输出.
有任何想法吗?
谢谢!!!
如果将exec()调用放在单独的脚本中,则可以使用并行多次调用该外部脚本curl_multi_exec().这样,您可以在单独的请求中进行所有调用,这样它们就可以同时执行.轮询&$still_running以查看所有请求何时完成,之后您可以收集每个请求的结果.
更新:这是一篇博文,详细描述了我所描述的内容.
基于上面链接的博客文章,我将以下示例放在一起.
脚本并行运行:
// waitAndDate.php
<?php
sleep((int)$_GET['time']);
printf('%d secs; %s', $_GET['time'], shell_exec('date'));
Run Code Online (Sandbox Code Playgroud)
脚本并行调用:
// 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);
Run Code Online (Sandbox Code Playgroud)
以下是我运行几次时收到的一些输出:
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
Run Code Online (Sandbox Code Playgroud)
希望有所帮助!
一方面注意:确保您的Web服务器可以处理这么多并行请求.如果它按顺序服务它们或者只能同时服务很少,那么这种方法很少或根本没有.:-)
PHP的exec函数将始终等待执行的响应.但是,您可以将进程的stdout和stderror发送到/ dev/null(在unix上),并使这些脚本几乎立即执行.这可以通过添加..
'> /dev/null 2>&1 &'
Run Code Online (Sandbox Code Playgroud)
到执行字符串的末尾.
但!这意味着他们将分开并独立完成处理.可能值得让他们在某处写回复.你可以创建一个监听器来挑选它并处理它.
| 归档时间: |
|
| 查看次数: |
11238 次 |
| 最近记录: |