#!/usr/bin/perl # This is asynctest.pl # Running under Apache 2.2/Linux. use strict; use warnings; use CGI; use IO::Handle; # Tried this: # STDOUT->autoflush(1); my $cgi=new CGI; unless ($cgi->param('a') eq 'foo') { my $pid=$cgi->param('pid'); print< foo

$pid

EOT exit; } else { # Tried this: $SIG{CHLD}='IGNORE'; if (my $pid=fork()) { # Redirect back to home screen, passing # the PID so we can keep an eye on it. print "Status: 302 Moved Temporarily\n"; print "Location: asynctest.pl?c=$pid\n\n"; # Tried this: # # my $old_fh=select(STDOUT); # $|=1; # select($old_fh); # And tried this: # # STDOUT->flush(); # And this: # # close STDOUT; # ...in varying combinations. # But this process always waits # for the sleep() in the other # process before sending the # redirect. exit; } else { close STDOUT; sleep(20); exit; } } #### #snipped if (my $pid=fork()) { $|=; # Set auto-flush. print "Status: 302 Moved Temporarily\n"; print "Location: asynctest.pl?c=$pid\n\n"; close STDOUT; exit; } else { # We don't really need the PID file, but # daemonize() complains if you don't provide it, # despite the manpage saying it's optional. my $pidfile="/var/tmp/async$$.pid"; my @current_user=getpwuid($<); daemonize($current_user[2],$current_user[3],$pidfile); sleep(20); unlink($pidfile); # otherwise it doesn't go away exit; }