JungleBoy has asked for the wisdom of the Perl Monks concerning the following question:

I've got a cgi script that takes a while to complete, and would like to output some indication of progress. My problem is that the output is being cached, and not displayed on the browser until everything is complete.

I've tried setting $| = 1, but it still doesn't appear to be doing what I want. The script currently outputs " x%..." where x is some multiple of 10 (i.e. 10%... 20%...).

Any suggestions?

Replies are listed 'Best First'.
Re: CGI progress indicator
by thraxil (Prior) on Oct 02, 2001 at 00:32 UTC

    this seems to work for me (in mozilla 0.9.4/linux and IE5.5/win2k):

    #!/usr/bin/perl -w use strict; use CGI qw/:standard /; $| = 1; print header(); print<<ENDHTML; <html><head><title>progress test</title></head> <body> ENDHTML foreach my $i (1..10) { print $i, "<br />\n"; sleep 3; } print "</body></html>"; exit 0;

    if you don't include the starting html tags, IE refuses to show anything until it's all done loading. maybe that's your problem?

    anders pearson

      Doh!
      I see my problem. The page is enclosed in a table, and it won't draw the table until the table is complete. Guess I have to do a little bit of redesigning.

      Thanks for the help.
      this works fine in mozilla, but has anyone got it working in ie6? i have the caching problem so everything is still displayed when execution is comlete. it's so anoying!
Re: CGI progress indicator
by merlyn (Sage) on Oct 02, 2001 at 01:17 UTC
      Being in awe of your experience, I'm hesitant to say otherwise, but in my experience,turning off buffering and printing some sort of progressing indicator, (.,%,etc) every x operation works 100% percent of the time in my experience and it's a lot simpler. I'm not saying this is the optimal solution for every problem, but I don't really see why it's not reliable in your opinion.

      -Lee

      "To be civilized is to deny one's nature."
        The problem here is that it depends upon your browser's behaviour. If you have a browser that wants to load the entire source before it starts rendering (lynx was and probably still is a good example of this), then the user will see nothing until the process has entirely finished. This is not a good thing.

        The other problem is that browsers will usually get bored and time-out if they don't see any connection on a socket for a long enough period of time, or if the connection remained open for too long. So your browser might get bored and drop the connection after 50% of the processing has been done, even if you have been sending "progress updates".

        So while you can have a solution that might work 100% of the time under your favourite or most popular browsers, it's not going to work 100% of the time with all browsers. This is where client-pull comes in handy, just about everything supports (or should support) refresh tags, so you don't have to worry about the user's browser getting bored, not displaying the progress reports, or wandering off to do other things.

        Cheers,
        Paul