PHP输出缓冲区未刷新 [英] PHP output buffer not flushing

查看:135
本文介绍了PHP输出缓冲区未刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多脚本会在执行时回显进度,因为它们运行时间很长.每个基本上都在处理的每个循环数据行的末尾执行以下操作:

I have a number of scripts that echo progress as they execute because they are long running. Each basically does the following at the end of each looped row of data processed:

echo '.';
@ob_flush();
flush();

多年来一直运行良好,然后我在多台服务器上升级到PHP 5.3.x和Apache2.2.x.现在,即使我用空白填充缓冲区或设置"ob_implicit_flush(1)",也无法通过命令显示输出.

This was working just fine for years and then I upgraded to PHP 5.3.x and Apache 2.2.x across several servers. Now, even if I pad the buffer with white space or set "ob_implicit_flush(1)", I can't get it to show the output on command.

一台服务器仍显示输出,但它是成块的.这可能需要将近5分钟,然后屏幕上突然出现一串点.在其他服务器上,脚本完全执行完毕之前,我什么也没得到.

One server still shows the output, but it is in chunks. It can take nearly 5 minutes and then suddenly a string of dots appears on the screen. With the other servers, I get nothing until the script finishes executing entirely.

我尝试浏览php.ini和httpd.conf文件,以查看是否可以确定不同服务器之间发生了什么变化,但显然缺少某些内容.

I've tried looking through the php.ini and httpd.conf files to see if I could figure out what had changed between the different servers, but I'm obviously missing something.

我还尝试为受影响的脚本在.htaccess中禁用mod_deflate,但这也无济于事(禁用mod_gzip可立即解决问题).

I've also tried disabling mod_deflate in .htaccess for the affected scripts, but that doesn't help either (disabling mod_gzip used to fix the problem right away).

有人可以为此指出正确的方向吗?无法实时监视脚本执行会导致各种问题,但我们不能再继续使用这些较旧的PHP版本.

Can someone point me in the right direction with this please? Not being able to monitor script execution in real time is causing all sorts of problems but we can't stay on these older PHP versions any longer.

更奇怪的是,我确实尝试将服务器降级到PHP 5.2.17,但是降级后仍然存在输出缓冲区问题.这使我怀疑这与自Apache 2被保留以来Apache处理PHP输出的方式有关.

On an even more peculiar side note, I did try downgrading a server to PHP 5.2.17 but the output buffer problem remained after the downgrade. This makes me suspect it is something relating to the way Apache is handling PHP output since Apache 2 was left in place.

推荐答案

ob_flush()(flush())仅刷新PHP缓冲区-Web服务器自行维护缓冲区.听起来很奇怪,但尽早刷新缓冲区实际上会降低服务器上的吞吐量,因此,较新版本的Apache缓冲区会更加积极.使用HTTP块编码时,还存在与压缩和部分渲染有关的可怕问题.

ob_flush() (an flush()) only flush the PHP buffer - the webserver maintains a buffer itself. And weird as it may sound, flushing the buffer early actually decreases throughput on the server, hence recent versions of apache buffer more agressively. There's also horrendous problems relating to compression and partial rendering when working with HTTP chunked encoding.

如果要向页面增量添加内容,请使用ajax或websocket一次将其添加一点.

If you want to incrementally add content to a page then use ajax or websockets to add it a bit at a time.

这篇关于PHP输出缓冲区未刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆