Gradual output from cgi script
Posted by iana (iana), 25 May 2004Hello 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?
Posted by admin (Graham Ellis), 25 May 2004There 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 2004Hi Graham,
I've made "Simulation Code":
This give the big pause, followed by all the output at once. Am I doing something silly here?
Posted by admin (Graham Ellis), 26 May 2004Hmm ... 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 2004Hi Graham,
Its the autoflush. Adding $|=1 to the script solved the problem instantly!
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: firstname.lastname@example.org • WEB: http://www.wellho.net • SKYPE: wellho