duanlang0025 2016-10-15 05:26
浏览 22

CakePHP中的连续输出

I have some long processing in loop and want to output progress contiguously.

while (true)
{
   $percents = $do_my_stuff();
   echo $percents;
   $this->log($percents);
}

I have tried many techniques, but still no luck

ini_set('output_buffering', 'off');
ini_set('zlib.output_compression', false);
while (@ob_end_flush());
ini_set('implicit_flush', true);
ob_implicit_flush(true);

flush() in each cycle

wrap loop in

$this->response->body(function () { /* loop with echo here */ }

but still browser displays all data once when script finished. Response headers:

HTTP/1.1 200 OK
Host: localhost:8765
Connection: close
X-Powered-By: PHP/5.6.20
Content-type: text/html; charset=UTF-8

PHP 5.6, built-in server using ./bin/cake server, CakePHP 3

How can i do that? I want my users see percents of operation because it can run for a long time.

  • 写回答

1条回答 默认 最新

  • dongwo2222 2016-10-27 14:10
    关注

    There is no way to enforce contiguous output to be displayed in a browser. Some browsers will not render until they've received the closing </html> tag. So to verify the output is working you need to use the network monitor to see if the server is streaming data.

    Also, do not use the response object from Cake. That is used to buffer the output and have it processed by events before sending to the client. You will need to manually send the data and end the response.

    header("Content-Type: text/plain");
    while (@ob_end_flush()); // Flush and disable output buffers
    for($a = 0; $a < 100; $a++) {
        echo "X";
        flush(); // Flush output
        sleep(1);
    }
    die; // end request here.
    

    If you want to show a progress bar on the client side for a long task. This is not the way to do it.

    You should write the progress to the session, save it, and then request via AJAX what that progress is.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题