[prev in list] [next in list] [prev in thread] [next in thread] 

List:       cgiapp
Subject:    Re: [cgiapp] Progress while loading db records - again
From:       George Hartzell <hartzell () alerce ! com>
Date:       2007-09-07 19:31:24
Message-ID: 18145.42764.958032.541323 () almost ! alerce ! com
[Download RAW message or body]

ademmler writes:
 > Hi C::A users!
 > 
 > I want to show a "progress indicator" while my cgi script gets  
 > information from my database.
 > I have read and tested all suggestions, which had been made here  
 > allready, but I can get it work.
 > I need help please.
 > 
 > What I wanna do is;
 > I have a submit button in my intial runmode.
 > After submit I get some information from a database.
 > 
 > sub my_runmode {
 > 
 > 	Update Page with "progressbar.gif" + some text
 > 
 > 	foreach $entry(@list) {
 > 		get_entry_from_database();
 > 		update_text_on_webpage
 > 	}
 > 
 > 	return(result);
 > }
 > 
 > 
 > Does somebody have a simple - live - example for me?

Remember that you can't push content up to the client like that, all
you can do is ask the client to come back and check if things are done
yet.

Here's a stand alone example that asks the client to come back and get
another page after 3 seconds (set via header_props in twiddle_page()).
If you start off at http://localhost:8080/dawdle you can watch "Passes
to go" count down from 3 (hardwired in twiddle()) to zero, then you'll
see a done page.

If you really have some long-running db query that you're doing,
you're going to need to find a way to run it asynchronously (in its
own thread or process) and each pass through your main run mode
(dawdle() in this example) you'll need to figure out how much progress
you've made and feed that into your twiddling output.

g.

use CGI::Application::Server;

use strict;
use warnings;

my $server = CGI::Application::Server->new();
$server->entry_points({'/dawdle' => 'Dawdle',
		      });
$server->run();
exit(0);

package Dawdle;

use strict;
use warnings;

use base 'CGI::Application';

sub setup {
  my $self = shift;
  $self->start_mode('dawdle');
  $self->run_modes([ 'dawdle' ]);
}

sub dawdle {
  my $self = shift;
  my $counter = $self->query->param('counter');
  my $html;

  $counter = 3 unless (defined($counter));

  if ($counter > 0) {
    $html = $self->twiddle_page(--$counter);
  }
  else {
    $html = $self->done_page();
  }

  return($html);
}

sub twiddle_page {
  my $self = shift;
  my $counter = shift;
  my $html = <<EODAWDLE;
<html>
<body>
<h1>Thanks for your patience</h1>
Passes to go: $counter
</body>
</html>
EODAWDLE

  $self->header_props(-refresh => "5;URL=/dawdle?counter=$counter"
		     );
  return($html);
}

sub done_page {
  my $self = shift;
  my $html = <<EODONE;
<html>
<body>
<h1>Phew.</h1>
DONE.
</body>
</html>
EODONE

  return($html);
}

---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/cgiapp@lists.erlbaum.net/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: cgiapp-unsubscribe@lists.erlbaum.net
For additional commands, e-mail: cgiapp-help@lists.erlbaum.net

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic