【问题标题】:Force continuous output with Codeigniter 3使用 Codeigniter 3 强制连续输出
【发布时间】:2016-07-04 11:35:29
【问题描述】:

我有一个应该永远运行的 codeigniter 控制器方法(无限循环) - 它检查一些数据库并进行一些数据维护。

在某些事件上,我想向浏览器输出(回显)一行,这样我的输出看起来有点像这样:

Service loop started
...task xxx done
...done something else
...found xyz
...task xxx done
...task xxx done
...task xxx done
...done something else

现在 Codeigniter 3 缓冲输出并等待方法完成,然后再将输出发送到浏览器。如何强制 CI3 刷新输出?

这是我的代码(简化):

public function dowork()
{
  $counter = 0;   // I am using a counter to test this with 1000 loops
   while ($counter <= 1000)
   {
     // do something here....

     $this->output->_display(); // this doesn't seem to work

     flush();            // I took this from the PHP manual
     ob_flush();         // but it doesn't send the output either

     $counter++;

   }
}

[注意可能需要补充一点,我在 Apache 2.4 / Ubuntu 16.04 LTS 和 PHP 5.6 上运行]

【问题讨论】:

    标签: php codeigniter output flush


    【解决方案1】:

    你需要提供一些东西来输出。函数set_output($output)append_output($output) 用于此目的。

    public function dowork()
    {
      $counter = 0;   // I am using a counter to test this with 1000 loops
    
      while($counter <= 1000)
      {
        //In addition to a "default" output, you should set headers too.
        $this->output
            ->set_status_header(200)
            ->set_content_type('text/html')
            ->set_output("Service loop $counter started"); 
    
        // do tasks here
    
        if($xxx)
        {
          // undocumented function append_output() appends data onto the output string.
          $this->output->append_output("task xxx done");
        }
    
        if($xyz)
        {
          $this->output->append_output("found xyz");
        }
    
        //tasks done, send to browser
        $this->output->_display();
    
        $counter++;
      }
    }
    

    您是否意识到此方法会阻止用户在完成之前执行任何其他任务?

    【讨论】:

    • 对不起,这不起作用。来自手册:Calling this method manually without aborting script execution will result in duplicated output.
    猜你喜欢
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    • 2016-03-10
    • 2022-11-05
    • 1970-01-01
    相关资源
    最近更新 更多