如何从 Apache 2 提供无缓冲的 CGI 内容?
我希望能够允许用户在生成长时间运行的 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
有任何想法吗?
I would like to be able to allow a user to view the output of a long-running GCI script as it is generated rather than after the script is complete. However even when I explicitly flush STDOUT the server seems to wait for the script to complete before sending the response to the client. This is on a Linux server running Apache 2.2.9.
Example 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."
Similar example in perl:
#!/usr/bin/perl
print "Content-type: text/plain\n\n";
for ($i = 1; $i <= 10 ; $i++) {
print "$i\n";
sleep(1);
}
print "Done.";
This link says as of Apache 1.3 CGI output should be unbuffered (but this might apply only to Apache 1.x): http://httpd.apache.org/docs/1.3/misc/FAQ-F.html#nph-scripts
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Randal Schwartz 的文章 通过 CGI 观看长进程 解释了一种不同的方式(恕我直言,更好)观察长时间运行过程的方式。
Randal Schwartz's article Watching long processes through CGI explains a different (and IMHO, better) way of watching a long running process.
刷新 STDOUT 会有所帮助。 例如,以下 Perl 程序应该按预期工作:
Flushing STDOUT can help. For example, the following Perl program should work as intended:
您必须将推送脚本放入包含特殊 .htaccess 的特殊目录中
在此环境规格下:
You must put your push script into a special directory wich contain a special .htaccess
with this environnment specs:
根据 CGI::Push,
你只需要在 python 中找到 do_push 等效项即可。
编辑:查看CherryPy:流式传输响应正文。
(来源:cherrypy.org)
According to CGI::Push,
You just have to find do_push equivalent in python.
Edit: Take a look at CherryPy: Streaming the response body.
(source: cherrypy.org)