Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Gradual output from cgi script

Posted by iana (iana), 25 May 2004
Hello all!

I have a CGI script which can take a long time to execute. Is there a way to output the progress gradually onto the viewing browser?

When I try to execute the CGI, there is a huge delay after which all the progress information comes out with the answer at once.

I'm looking for a way to notify that progress is being made, and that we are 20% done... etc.

Is this possible?

Kind Regards,
Ian./

Posted by admin (Graham Ellis), 25 May 2004
There are all sorts of "clever" ways of doing this, and some simple ones too that might work for you ...

The usual cause of "all at once" page display is that something in the HTML that you're sending delays the display because the browser is missing a vital piece of information ... the most common on these is a page that comprises a single table with widths not specified for the columns ... the poor browser has to receive the whol epage before it knows what to display.

Of course, you'll first want to establish if you have this problem. Try adding a test line at the start of your CGI script that prints out "hello world" followed by a <hr> to make 100% sure that the line's ended ... then run the CGI script.

I do suspect that your "huge delay" is while HTML is being received and stored by the browser - it's certainly the most common cause of this sort of thing - but IF that perl script really is burning up a lot of resources, please post a follow up and I'll outline multipart documents where you can send a holder message followed by a complete page.

Posted by iana (iana), 25 May 2004
Hi Graham,

I've made "Simulation Code":

Code:
#!c:/Perl/bin/Perl.exe


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

print "Hello World<hr>\n";
sleep(1); #simulate work

print "Hello World<hr>\n";
sleep(1); #simulate work

print "Done<hr>\n";

This give the big pause, followed by all the output at once. Am I doing something silly here?

Ian./

Posted by admin (Graham Ellis), 26 May 2004
Hmm ... couple of (as in 3 or 4!) possibilities ....

1.  Perl tends to flush its buffers at the end of each line of output, and I see that you're sending \n when the Windows EOL is \r\n.  Solution if this is the problem is to turn autoflush mode one (add a line $| = 1;  at the top of your script).

There's a sample program to play with from the command line with $| on our web site at http://www.wellho.net/resources/ex.php4?item=p210/ticker - run it with a command lline parameter of 0 for regular operation, and with a command line parameter of 1 for autoflush mode to be on

2. It's still possible that the browser is buffering before it displays - stranger things have happened.  Try visiting the page with telnet:
     telnet yourhostname 80
     GET /cgi-bin/scriptname.pl HTTP/1.0
followed by TWO new line characters.  You should see you page come out in sections every second.  If so but the display is all in a burst, it's the browser.   If the page still comes all at once, it's a server side issue you're looking for.

3. You might like to try a non-parsed header script - see the example we have online at http://www.wellho.net/cgi-bin/net/sample.pl?which=nph-clock.pl.  This is a very old example and the page decoration looks poor at the moment, but the example is good.  Note that nph scripts must have names starting with "nph" and you're responsible for generating the entire headers.

4. The web server could  STILL be going the buffering ... even after all of these!

Hope this all makes sense.  If one of these solves it (or if not!) please follow up so that I can learn which issues it was and wasn't.

Modified to add ... (a) Page decoration on the old example fixed. (b) Note - the "nph" example runs the operating system's uptime command once a minute - the purpose of the script is to give a web page based monitor of changing activity.  This limits the script to Linux / Unix systems. For Windows use as a demo, choose some other command line based command that you would like to run every minute  

Posted by iana (iana), 26 May 2004
Hi Graham,

Its the autoflush. Adding $|=1 to the script solved the problem instantly!

Thankyou.

Ian./



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho