<small id='B0tm2'></small><noframes id='B0tm2'>

    <i id='B0tm2'><tr id='B0tm2'><dt id='B0tm2'><q id='B0tm2'><span id='B0tm2'><b id='B0tm2'><form id='B0tm2'><ins id='B0tm2'></ins><ul id='B0tm2'></ul><sub id='B0tm2'></sub></form><legend id='B0tm2'></legend><bdo id='B0tm2'><pre id='B0tm2'><center id='B0tm2'></center></pre></bdo></b><th id='B0tm2'></th></span></q></dt></tr></i><div id='B0tm2'><tfoot id='B0tm2'></tfoot><dl id='B0tm2'><fieldset id='B0tm2'></fieldset></dl></div>
    <legend id='B0tm2'><style id='B0tm2'><dir id='B0tm2'><q id='B0tm2'></q></dir></style></legend>

      <bdo id='B0tm2'></bdo><ul id='B0tm2'></ul>
  1. <tfoot id='B0tm2'></tfoot>

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

      Run multiple exec commands at once (But wait for the last one to finish)(一次运行多个 exec 命令(但要等待最后一个完成))
    1. <legend id='Y2ooP'><style id='Y2ooP'><dir id='Y2ooP'><q id='Y2ooP'></q></dir></style></legend>
    2. <i id='Y2ooP'><tr id='Y2ooP'><dt id='Y2ooP'><q id='Y2ooP'><span id='Y2ooP'><b id='Y2ooP'><form id='Y2ooP'><ins id='Y2ooP'></ins><ul id='Y2ooP'></ul><sub id='Y2ooP'></sub></form><legend id='Y2ooP'></legend><bdo id='Y2ooP'><pre id='Y2ooP'><center id='Y2ooP'></center></pre></bdo></b><th id='Y2ooP'></th></span></q></dt></tr></i><div id='Y2ooP'><tfoot id='Y2ooP'></tfoot><dl id='Y2ooP'><fieldset id='Y2ooP'></fieldset></dl></div>
          <tbody id='Y2ooP'></tbody>

            <bdo id='Y2ooP'></bdo><ul id='Y2ooP'></ul>

                <tfoot id='Y2ooP'></tfoot>

                <small id='Y2ooP'></small><noframes id='Y2ooP'>

              • 本文介绍了一次运行多个 exec 命令(但要等待最后一个完成)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我环顾四周,但似乎找不到任何人正试图完全按照我的意愿行事.

                I've looked around for this and I can't seem to find anyone who is trying to do exactly what I am.

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

                I have information that is passed in to my function via a _POST request. Based on that data, I run an exec command to run a TCL script a certain number of times (with different parameters, based on the post variable). Right now, I have the exec in a foreach so this takes forever to run (the TCL script takes 15 or so seconds to come back, so if I need to run it 100 times, I have a bit of an issue). Here is my code:

                    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);
                }
                

                显然,这段代码只是删除了大块的摘录,但希望它足以证明我正在尝试做的事情.

                Obviously this code is just an excerpt with big chunks removed, but hopefully it's enough to demonstrate what I'm trying to do.

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

                I need to run all of the execs at once and I need to wait for them all to complete before returning. I also need the output of all of the scripts stored in the array called $execout.

                有什么想法吗?

                谢谢!!!

                推荐答案

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

                If you put your exec() call in a separate script, you can call that external script multiple times in parallel using curl_multi_exec(). That way, you'd make all the calls in separate requests, so they could execute simultaneously. Poll &$still_running to see when all requests have finished, after which you can collect the results from each.

                更新:这里是 一篇博文 详细说明了我所描述的内容.

                Update: Here's a blog post detailing exactly what I'm describing.

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

                Based on the blog post linked above, I put together the following example.

                脚本并行运行:

                // 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
                ", microtime(true) - $start);
                

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

                Here is some output I received when running it a few times:

                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 服务器可以处理这么多并行请求.如果它按顺序为它们提供服务,或者只能同时提供很少的服务,则这种方法几乎没有收益或没有收益.:-)

                One side note: make sure your web server can process this many parallel requests. If it serves them sequentially or can only serve very few simultaneously, this approach gains you little or nothing. :-)

                这篇关于一次运行多个 exec 命令(但要等待最后一个完成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
              • <legend id='v4W3V'><style id='v4W3V'><dir id='v4W3V'><q id='v4W3V'></q></dir></style></legend>
                    <bdo id='v4W3V'></bdo><ul id='v4W3V'></ul>
                  • <small id='v4W3V'></small><noframes id='v4W3V'>

                    <i id='v4W3V'><tr id='v4W3V'><dt id='v4W3V'><q id='v4W3V'><span id='v4W3V'><b id='v4W3V'><form id='v4W3V'><ins id='v4W3V'></ins><ul id='v4W3V'></ul><sub id='v4W3V'></sub></form><legend id='v4W3V'></legend><bdo id='v4W3V'><pre id='v4W3V'><center id='v4W3V'></center></pre></bdo></b><th id='v4W3V'></th></span></q></dt></tr></i><div id='v4W3V'><tfoot id='v4W3V'></tfoot><dl id='v4W3V'><fieldset id='v4W3V'></fieldset></dl></div>

                      • <tfoot id='v4W3V'></tfoot>
                          <tbody id='v4W3V'></tbody>