我想允许用户在长时间运行的GCI脚本生成输出时就能查看它,而不是等到脚本完成后再查看。但是,即使我显式地刷新了STDOUT,服务器似乎仍然会等待脚本完成后才将响应发送给客户端。这是在运行Apache 2.2.9的Linux服务器上发生的。
示例Python CGI:
#!/usr/bin/python
import time
import sys
print "Content-type: text/plain"
print
for i in range(1, 10):
print i
sys.stdout.flush()
time.sleep(1)
print "Done."
在perl中有一个类似的例子:
#!/usr/bin/perl
print "Content-type: text/plain\n\n";
for ($i = 1; $i <= 10 ; $i++) {
print "$i\n";
sleep(1);
}
print "Done.";
这个链接说自 Apache 1.3 版本起,CGI 输出应该是非缓冲的(但这可能只适用于 Apache 1.x):http://httpd.apache.org/docs/1.3/misc/FAQ-F.html#nph-scripts
有什么想法吗?