dongliaojing0554 2014-06-18 18:48
浏览 58

计算每个执行步骤的响应的大任务

Sorry if this topic already exists, but I couldn't find the solution. I have some calculations processed on server side. These calculations consists of several steps. I would like a response for each executed step. For instance, there are 5 steps in a task, and server must send me 5 responses:

  • Step 1 is executed
  • Step 2 is executed
  • Step 3 is executed
  • ...etc

I read a lot of forums and blogs, but I can't solve this problem. In my case I think it is better to use long polling in comet. I wrote a simple example of my task (posted below); it doesn't work. This task has button which starts the counter (from 1 to 11 with sleep 1 sec), then Ajax sends a requests. The server must give the accumulated text ($q_msg), clear this variable, and continue to accumulate another counts. But instead of this, server accumulates everything without any responses and at the end response all in an answer. So, where is my fault? Please, assist me.

HTML:

        <script type="text/javascript">
        var request;
        function createXMLRequest() {
            request = new XMLHttpRequest();
            return request;
        }

        //start counter
        function comet_btnStart() {
            document.getElementById('count1').innerHTML = "";
            createXMLRequest();
            var param = "btnStart=pusk";
            request.open("GET", "counter.php?" + param, true);
            request.onreadystatechange = comet_response;
            request.send(null);
        }

        //get values
        var q = "";
        function comet() {                
            createXMLRequest();
            var param = "q=" + q;
            request.open("GET", "counter.php?" + param, true);
            request.onreadystatechange = comet_response;
            request.send(null);
        }

        function comet_response() {
            if (request.readyState === 4)
            {
                if (request.status === 200)
                {
                    if (request.responseText !== null)
                    {
                        var text = request.responseText;
                        if (text === "end") document.getElementById('count1').innerHTML += "<br/>Task is executed";
                        else 
                        {
                            var result = text.split(",");
                            q = result[result.length-1];
                            document.getElementById('count1').innerHTML += "<br/>" + request.responseText;
                            setTimeout(comet, 2000);
                        }
                    }
                    else document.getElementById('count1').innerHTML += "<br/>No response";
                }
                else document.getElementById('count1').innerHTML += "<br/>Invalid connection";
            }
        }
    </script>
</head>
<body>
    <div>
        Click button to start counter: <input type="button" value="Начать" name="btnStart" id="btnStart" onclick="comet_btnStart()"  /><br/><br/>
        Counts:<br/> <span id='count1' style='color: #246499; font-weight: bold'></span>
    </div>
</body>



PHP:

<?php
if (isset($_GET['btnStart']))
{
    $q_msg = "";
    $q = 0;
    logic::counter();

    echo $q_msg;
    $q_msg = "";
}

if (isset($_GET['q']))
{
    $lastq = $_GET['q'];
    global $q;
    if ($lastq < $q)
    {
    echo $q_msg;
        $q_msg = "";
    }
    else echo "end";
}

class logic
{    
    static function counter() 
    {
        global $q;
    while ($q <= 10)
    {
        sleep(1);
        $q++;
        global $q_msg;
        if (strlen($q_msg) > 0) $q_msg .= ", " . $q; 
        else $q_msg = $q; 
        //just for debugging
        echo $q_msg; //why server doesn't send data???
    }
    }    
}
  • 写回答

1条回答 默认 最新

  • duanjumie8753 2014-06-18 18:53
    关注
    $msc=microtime(true);
    

    //executing code

    $msc=microtime(true)-$msc;
    echo $msc.' seconds'; // in seconds
    echo ($msc*1000).' milliseconds'; // in millseconds
    

    i dont know if this is what you are looking for. But if i understsand correct this is what you are looking for. You can read more here How to get the execution time of a MySQL query from PHP?

    Credit to Christian

    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题