我怎样才能不断地从一个Perl CGI脚本通知进度的用户? [英] How can I continuously inform the user of progress from a Perl CGI script?

查看:102
本文介绍了我怎样才能不断地从一个Perl CGI脚本通知进度的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的Perl脚本坐在我的Apache服务器的CGI-bin文件夹:

I have this Perl script sitting in the cgi-bin folder of my Apache server:

#!/usr/bin/perl 
use strict;
use warnings;

$| = 1;

print "Content-type: text/html\r\n\r\n";
print "Hello there!<br />\nJust testing .<br />\n";

my $top = 5;
foreach (1..$top) {
    print "i = $_<br />\n";
    sleep 1;
}

我想在这里实现的是网页的更新逐渐显示给用户的更新状态。然而,我真的开始就是整个输出一次,5秒的延迟之后。

What I want to achieve here is a gradual update of the web page to show the user an updated status. However, what I'm actually getting is the entire output at once, after a delay of 5 secs.

有什么办法我可以写一个脚本,能够不断告知其进度用户?我有一个脚本,需要很长的时间才能完成,我希望能看到它的实时而不是整个脚本来完成进度。

Is there any way I can write a script that is able to continuously inform the user of its progress? I have a script that takes a long time to finish and I would like to be able to see its progress in real time rather than the whole script to finish.

我也试图设置自动冲洗模式,关闭($ | = 0),但即使没有做任何事情。

I have also tried to set autoflush mode to off ($| = 0), but even that doesn't do any thing.

推荐答案

兰德尔·施瓦茨显示一种更好的方式看长通过CGI 流程。

Randal Schwartz shows a better way in Watching long processes through CGI.

这是说,卧谈的内容类型也无济于事。下面的脚本不正是你似乎什么希望它在Windows XP上使用Apache 2.2(因为它需要十秒输出的最后一行出现)做的:

That said, lying about the content type will not help. The following script does exactly what you seem to want it to do on Windows XP with Apache 2.2 (in that it takes ten seconds for the last line of output to appear):

#!/usr/bin/perl

use strict;
use warnings;

use CGI qw(:cgi);

print header('text/plain');

$| = 1;

print "Job started\n";

for ( 1 .. 10 ) {
    print "$_\n";
    sleep 1;
}

现在,如果您要发送HTML,你无法回避的事实,大多数浏览器会等到他们认为是足够的页面开始呈现。

Now, if you are sending HTML, you cannot avoid the fact that most browsers will wait until they what they believe is enough of the page to begin rendering.

这篇关于我怎样才能不断地从一个Perl CGI脚本通知进度的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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