perl-embperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sherwin Daganato <sher...@saturn.emc.com.ph>
Subject Re: Immediate response for slow POSTs
Date Wed, 31 Mar 2004 18:43:17 GMT
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


Mime
View raw message