You are viewing a plain text version of this content. The canonical link for it is here.
Posted to embperl@perl.apache.org by Paul Sharpe <pa...@russellsharpe.com> on 2004/03/26 00:05:29 UTC

Immediate response for slow POSTs

I'm running HTML::Embperl 1.3.X under mod_perl.  I have a POST form 
which will potentially be slow to send all of it's data.  How can I 
provide an 'in progress' response as soon as the request is received and 
replace it with the 'real' response once the POST data has been received 
and processed?

paul

-- 
Paul Sharpe                      Tel: 619 523 0100 Fax: 619 523 0101
Russell Sharpe, Inc              mailto:paul@russellsharpe.com
4993 Niagara Avenue, Suite 209   http://www.russellsharpe.com/
San Diego, CA 92107-3185



---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


Re: Immediate response for slow POSTs

Posted by Sherwin Daganato <sh...@saturn.emc.com.ph>.
On Thu, Mar 25, 2004 at 03:05:29PM -0800, Paul Sharpe wrote:
> I'm running HTML::Embperl 1.3.X under mod_perl.  I have a POST form 
> which will potentially be slow to send all of it's data.  How can I 
> provide an 'in progress' response as soon as the request is received and 
> replace it with the 'real' response once the POST data has been received 
> and processed?
> 
This is possible. Merlyn was able to do it in CGI. :-)

FROM http://www.stonehenge.com/merlyn/LinuxMag/col39.html:
The initial request sets up a forked process to perform the real work,
and redirects the browser to a new URL which will ''pull'' the results
obtained so far. If the results are incomplete, an additional header
instructs the browser to ''refresh'' the data after some number of
seconds.

You can do the same with mod_perl/HTML::Embperl. But instead of forking
the processing of the POST data, set it up with $r->register_cleanup.
For example, if you port Merlyn's program into embperl, you will have to
replace line 31 to 51 with something like this:

    $req_rec->register_cleanup(sub { # do the long process here
      unless (open F, "-|") {
        open STDERR, ">&=1";
        exec "/usr/sbin/traceroute", $host;
        my_die("Cannot execute traceroute: $!");
      }
      my $buf = "";
      while (<F>) {
        $buf .= $_;
        $cache->set($session, [0, $buf]);
      }
      $cache->set($session, [1, $buf]);
    });

    $http_headers_out{'Location'} =
      qq/$ENV{'SCRIPT_URI'}?session=$session/;
    Apache::exit; # don't use the normal Perl exit() here. if you do,
                  # embperl will wait for the cleanup handler to finish.

More on why you should avoid forking in mod_perl here:
http://perl.apache.org/docs/1.0/guide/performance.html#Forking_and_Executing_Subprocesses_from_mod_perl

HTH

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org