简体   繁体   English

如何从Apache 2提供未缓冲的CGI内容?

[英]How can I serve unbuffered CGI content from Apache 2?

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. 我希望能够允许用户在生成长期运行的GCI脚本时而不是在脚本完成之后查看其输出。 However even when I explicitly flush STDOUT the server seems to wait for the script to complete before sending the response to the client. 但是,即使当我显式刷新STDOUT时,服务器似乎也要等待脚本完成才能将响应发送给客户端。 This is on a Linux server running Apache 2.2.9. 这是在运行Apache 2.2.9的Linux服务器上。

Example python CGI: 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: 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 此链接说,自Apache 1.3起,CGI输出应为无缓冲的(但这可能仅适用于Apache 1.x): http : //httpd.apache.org/docs/1.3/misc/FAQ-F.html#nph-scripts

Any ideas? 有任何想法吗?

Randal Schwartz的文章“ 通过CGI监视长期过程”解释了一种观察长期运行过程的不同方法(更好的是IMHO)。

You must put your push script into a special directory wich contain a special .htaccess with this environnment specs: 您必须将推送脚本放入一个特殊的目录,该目录包含具有以下环境规范的特殊.htaccess:

Options +ExecCGI
AddHandler cgi-script .cgi .sh .pl .py
SetEnvIfNoCase Content-Type \
"^multipart/form-data;" "MODSEC_NOPOSTBUFFERING=Do not buffer file uploads"
SetEnv no-gzip dont-vary

According to CGI::Push , 根据CGI :: Push

Apache web server from version 1.3b2 on does not need server push scripts installed as NPH scripts: the -nph parameter to do_push() may be set to a false value to disable the extra headers needed by an NPH script. 从1.3b2版开始的Apache Web服务器不需要将服务器推送脚本安装为NPH脚本:do_push()的-nph参数可以设置为false值,以禁用NPH脚本所需的额外头。

You just have to find do_push equivalent in python. 您只需要在python中找到do_push等效项即可。

Edit : Take a look at CherryPy: Streaming the response body . 编辑 :看一下CherryPy:流式响应正文

When you set the config entry "response.stream" to True (and use "yield") CherryPy manages the conversation between the HTTP server and your code like this: 当您将配置条目“ response.stream”设置为True(并使用“ yield”)时,CherryPy将管理HTTP服务器与您的代码之间的对话,如下所示:

替代文字
(source: cherrypy.org ) (来源: cherrypy.org

Flushing STDOUT can help. 冲洗STDOUT会有所帮助。 For example, the following Perl program should work as intended: 例如,以下Perl程序应按预期运行:

#!/usr/bin/perl

use strict;
use warnings;

local $| = 1;

print "Content-type: text/plain\n\n";

for ( my $i = 1 ; $i <= 10 ; $i++ ) {
    print "$i\n";
    sleep(1);
}

print "Done.";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM