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

查看:33
本文介绍了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 和 Apache 2.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 缓冲区 - 网络服务器自己维护一个缓冲区.听起来很奇怪,提前刷新缓冲区实际上会降低服务器上的吞吐量,因此最近版本的 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 或 websockets 一次添加一点.

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天全站免登陆