Link to home
Start Free TrialLog in
Avatar of tomaugerdotcom
tomaugerdotcomFlag for Canada

asked on

Unbuffered output to browser while CGI script is still processing

Hey folks - first question here (it's so exciting!)

I have a CGI script (Perl) running a long process but I want the web browser to display the partial output rather than what it does now which is wait until the entire process is finished (which sometimes times out) before sending the complete output.

And just to stave off a bunch of false leads, $| = 1 is already set and it doesn't solve the problem.

Running Apache on a Windoze server.

Here's a test script. If we can get this working, we've got a solution!

#! perl.exe # I know I know. What am I doing on a Windoze server? never you mind that. :)

$| = 1; # disable STDOUT buffering

print "Content-type: text/html\n\n"; # mime type
for (my $i = 1; $i <= 20; $i++) { # loop 20 times
  print "$i <br /> \r\n"; # print a line at a time - note the newline
  my $time = time; until (time = $time+1) {} # wait 1 sec. There's probably a better way to do this but WTFC right?
}

# -------------------- END OF EXAMPLE --------------------

So currently, this thing waits 20 secs then outputs all 20 lines. I want it to display 1 line a second. Here's my thought: Perl isn't buffering it, Apache is. Please note that I do not have access to the Apache environment configuration, so any solution must not assume that I can tweak Apache itself.

Thanks! I look forward to some enlightening responses!

Tom
SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tintin
Tintin

Worth having a read of the last section of http://www.hk8.org/old_web/linux/cgi/ch03_03.htm
Avatar of tomaugerdotcom

ASKER

Thanks folks for your responses so far. I need a little more direction on this one I think.

From the oriley book mentioned by Tintin:

"nph scripts were more common in the past, because versions of Apache prior to 1.3 buffered the output of standard CGI scripts ... However Apache 1.3 no longer buffers CGI output, so this feature of nph scripts is no longer needed with Apache"

So one would think that I wouldn't be experiencing this issue. For some reason I am. Is it a windows issue?

ahoffman mentioned mod_perl in Apache. Is this something that I can access using the Apache module from within my Perl script? Or is this something I have to do directly on the server (which I don't have administrative access on)? Some more details would be very much appreciated.

Thanks!

Tom
AFAIK you get something in your browser if the CGI send about 8k data before it times out.

>  Is this something that I can access using the Apache module from within my Perl script?
mod_perl loads the perl interpreter permantly into memory, so that apache does not need to start it for each (perl) CGI. AFAIK all Apache::* modules make sense with mod_perl only.
mod_perl can do much more than just imroving performance, if your scripts are programmed with mod_perl in mind (hence using Apache::*) then there is more data available, or it's easyer to get it, or ...
One thing that you could try (it depends on your server config which I know you don't have access to but it may be set up correctly) is renaming your cgi to begin with 'nph-'. This stands for non-parsed header. Basically it tells apache not to buffer the output and just to send exactly what the script outputs when it outputs it. You need to include the full HTTP header if you have a script like this.
I still haven't got this working, but I'm going to split points between Tintin and ahoffman for getting an answer in so promptly.

Apparently some of my assumptions were wrong - in fact, I have been informed that we are running Win3K server and ii6, NOT Apache, so in all likelihood the solutions posted above might indeed work on Apache, though they haven't worked in my case.

Thank you to all who took the time to respond to my query.

Tom