无法在 Windows 中使用flush() [英] Can't get flush() working in Windows

查看:33
本文介绍了无法在 Windows 中使用flush()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个 ubuntu 测试服务器,这个简单的脚本在每秒向浏览器输出一个数字的地方运行良好

We have a ubuntu test server that this simple script works fine on where it outputs a number each second to the browser

<?
    for ($i=0; $i<3; $i++) 
    {
        echo $i.'<br>';
        flush();
        sleep(1);
    }

但是在我的本地开发箱(windows 环境)上我无法让它工作,它在 3 秒后输出它.

However on my local development box (windows environment) I cannot get it to work, it outputs it all after 3 seconds.

我在本地机器上使用相同的 php 版本 (5.3.13) 和 apache (2.2.22) 版本作为测试服务器.我已经镜像了 php.ini 和 httpd.conf 中的大部分设置这是我设置的设置(与测试服务器相同)

I am using the same php version (5.3.13) as well as apache (2.2.22) version on my local box as the test server. I have mirrored most of the settings in both php.ini as well as the httpd.conf Here are settings I have set (which are identical to the test server)

output_buffering 0
zlib.output_compression = Off
zlib.output_compression_level = -1

我没有安装 mod_gzip.

I don't have mod_gzip installed.

我什至尝试在我的盒子上安装 wamp,但它也不起作用.

I have even tried doing a wamp install on my box, and it didn't work with that either.

我无法想象这是一个客户端(浏览器——firefox 13)问题,因为我使用完全相同的浏览器在测试服务器上查看脚本并且它工作正常

I can't imagine it being a client (browser -- firefox 13) issue as I use the exact same browser to view the script on the test server and it works fine

我尝试过启用隐式刷新、执行 ob_flush 等.没有运气.

I have tried enabling implicit flush, doing ob_flush, etc. No luck.

有人对如何使这项工作有任何想法吗?

Anyone have any ideas on how to get this working?

推荐答案

请记住@Wrikken 所说的,不推荐这样做.

Just keep in mind what @Wrikken said, this is not recommended.

所以我在想,你所做的一切似乎都是对的.我复制了您的设置并尝试了.我遇到了同样的问题.我从命令行执行了脚本,它起作用了.

So I was thinking, everything you're doing seems to be right. I copied your setup and tried. I faced same problem. I executed the script from command line, it worked.

然后我不得不做最后一个测试,Wireshark.在前几个数据包之后,很明显服务器正在正确发送所有内容,因此它必须是浏览器的缓冲区.

Then I had to do the last test, Wireshark. Following the first couple of packets it was clear that the server is sending everything correctly, so it had to be the browser’s buffer.

所以我尝试在循环之前发送一些数据,你猜怎么着?成功了!

So I tried send some data before the loop, well guess what? It worked!

给你:

<?php
ini_set('output_buffering','off');
ini_set('zlib.output_compression', 0);
echo str_repeat(" ", 1024), "\n";
for($i=0;$i<6;$i++) {
      echo $i."<br />\n";
      ob_flush();
      flush();
      sleep(1);
}
?>

我不确定您心目中的应用程序,但这显然不是一个好主意,我强烈建议您考虑其他选项.

I'm not sure about the application you have in mind, but this is clearly not a good idea, I highly recommend that you look into other options.

更新:经过长时间的 Google 搜索,我找到了 this这个

Update: After a lengthy Google search I found this and this

因为浏览器不知道如何正确呈现页面为了构造页面的字符,大多数浏览器缓冲了一定的执行任何 JavaScript 或绘制页面之前的字节数

Because a browser cannot correctly render a page without knowing how to construct the page's characters, most browsers buffer a certain number of bytes before executing any JavaScript or drawing the page

为了避免这些延迟,您应该始终指定字符对于任何大于 1 KB 的 HTML 文档,尽早编码(1024 字节,准确地说,这是我们测试过的任何浏览器).

To avoid these delays, you should always specify the character encoding as early as possible, for any HTML document larger than 1 KB (1024 bytes, to be precise, which is the maximum buffer limit used by any of the browsers we have tested).

所以我尝试先发送字符集而不是填充缓冲区:(并且成功了)

So I tried to send the charset first instead of filling the buffer: (and it worked)

<?php
ini_set('output_buffering','off');
ini_set('zlib.output_compression', 0);
//echo str_repeat(" ", 1024), "\n";
header('Content-Type: text/html; charset=iso-8859-1');
//Note that it shoudn't matter which charset you send
for($i=0;$i<6;$i++) {
      echo $i."<br />\n";
      ob_flush();
      flush();
      sleep(1);
}
?>

那么为什么它可以在第一台服务器上工作,而在第二台服务器上却不能工作?

很可能是因为您的第一台服务器正在发送带有标头的字符集,而第二台则没有.

Most likely it's because your first server is sending the charset with the header while the second one isn't.

但是,对于您的情况,我会进行以下更改

In your case, however, I'd make the following changes

<?php
ini_set('output_buffering','off');
ini_set('zlib.output_compression', 0);
//Plain text MIME type since you'll use for logging purposes
//and if you run it from CLI, you can ignore the whole header line
header('Content-Type: text/plain; charset=iso-8859-1');
for($i=0;$i<6;$i++) {
      //No need to echo <br /> once you'll run it from CLI
      echo $i."\n";
      ob_flush();
      flush();
      sleep(1);
}
?>

这篇关于无法在 Windows 中使用flush()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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