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

Dear Monks,
I guess this was probably questioned many times. But I did not find a working solution.
My code has computational overhead and I would like to inform the client that it should wait. The logic of my code is simple. After completing main_form() on the main page SUBMIT sends POST message. Then
if ($submit) { process_form( ...................
process_form() performs calculation based on recieved
$query->param(".....")
and then generates HTML whith computed results.
When computations are being performed main_form() is still shown.
I would like to show Wait .... message instead which will be interrupted when new HTML in iprocess_form() is generated.
There should be a simple solution. Your help is very much appreciated.

Replies are listed 'Best First'.
Re: Wait ... Loading ... message in Perl/CGI
by jettero (Monsignor) on Jan 25, 2008 at 21:30 UTC

    I may have misunderstood, but it sounded like you're waiting for a long process to finish before you can draw the rest of the html? Please correct me if I'm wrong.

    I really hate to link offsite, but merlyn is special: http://www.stonehenge.com/merlyn/LinuxMag/col39.html

    Perhaps you just want AJAX... that would change my answer (JSON, jquery).

    -Paul

      Correct, I am waiting for a long process to finish before I can draw the rest of the html
Re: Wait ... Loading ... message in Perl/CGI
by Joost (Canon) on Jan 25, 2008 at 21:36 UTC
    The simplest way to go about this (assuming you're sending out HTML) is to send out the "wait..." message in a div or some other structure containing an id attribute, and then when the final output is ready, send out a javascript snippet to remove or make invisible the wait... message. Or just use CSS that places the result right over the message.

    This may need some tweaking with buffering, but it works. If the wait time is very long, you will still need to make sure that if the user stops / reloads the page it won't mess things up.

      I have always wanted to do this on regular script (none html)
      can someone please let me know how to do this the right way?
      my incorrect version is
      use strict; sub clear_screen { system("clear"); print "wait......................\n"; } sub doit { my $yahoo; while ($yahoo < 100) { clear_screen(); $yahoo++; } print "\$yahoo is $yahoo\n"; } doit();
        You have to learn about fork-ing and managing child processes. Here's some example code:

        use POSIX qw(:sys_wait_h); sub doit { my $pid = fork(); unless (defined($pid)) { die "unable to fork: $!\n"; } if ($pid) { # parent # wait for child to finish and display waiting message my $start = time; while (1) { print "time spent waiting: ", time - $start, "\n"; last if (waitpid($pid, WNOHANG) > 0); sleep(2); } # child process has terminated # read results from file open(R, "<results-from-child-process"); while (<R>) { ... process results... }; close(R); } else { # child # perform time consuming computation here. ... # leave results in some file open(R, ">results-from-child-process"); print R ...; close(R); exit(0); } }