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

I'm not sure if I'm in the right place but I need help with my perl/CGI script. I just realised that for Perl/CGI scripts, it doesn't print webpages until the whole script has been processed. Is there a way of overcoming this? I have a script that takes a very long time (it downloads files and processes them) to run and I want it to show webpages to tell the user that the system hasn't hung on them. Anyone can enlightern me? Thanks.

Replies are listed 'Best First'.
Re: Perl/CGI scripts
by Joost (Canon) on Apr 29, 2005 at 09:44 UTC
    if you disable output buffering, the pages should be sent to the browser more or less as soon as you print them. Note that the server can also wait for some minimal amount of output before sending anything, and that some browsers will not show tables (and maybe other elements too) until they're fully recieved.

    To disable output buffering put

    $|=1;
    at the top of your script.

    See also perlvar.

      ++Joost, I can't believe I just stumbled across this at exactly the right time.

      I have never had this problem, but just switch to Pair Networks and all my Perl/CGI/HTML::Template page loads were taking forever. My customers were not happy. So, I added the $|=1 to the start of all my scripts and magic! Problem solved. Thanks.


      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: Perl/CGI scripts
by gellyfish (Monsignor) on Apr 29, 2005 at 11:43 UTC

    An alternate if you have some long processing that can continue without any further feedback to the user after sending "Thanks for your submission" is to fork a process to the long processing and close all of the STDIO handles in that process - in that way the main process will end and the server close the connection to the browser but you can continue processing:

    #!/usr/bin/perl -w + use CGI qw(:standard); + local $| = 1; + print header("text/plain"), "this is a test"; + if (!fork ) { close STDOUT; close STDIN; close STDERR; + # some long running processing ... for ( 1 ... 1000 ) { sleep 1; } }
    Of course if you are getting a lot of hits and your processing is taking a very long time, you may will find yourself getting complaints from the administrator of the server.

    /J\

Re: Perl/CGI scripts
by gman (Friar) on Apr 29, 2005 at 15:51 UTC
Re: Perl/CGI scripts
by kprasanna_79 (Hermit) on Apr 29, 2005 at 10:00 UTC
    Hai

    I understood your problem, usually in web based or browser based projects and
    if it takes some time to load page(includes backend processing), it is designed to show some graphics work(ex. progress bar) indicating the processing. It makes the user to feel that process is going on.

    And user also can avoid seeing dirty stuffs displayed, half processed.
    I think this make sense and this idea is used in major web sites(ex: yahoo while file upload)

    --prasanna.k
Re: Perl/CGI scripts
by sandrider (Acolyte) on May 05, 2005 at 06:03 UTC
    Thanks everyone for your help. My scripts work now.