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

Fellow monks,

In theory this CGI script should redirect the user's browser to a page while the script mails a newsletter in the background. However, the redirect does not take place until the mailing is finished. Why?

if (defined(my $pid=fork())) { if ($pid) { # $pid is not 0, so this is the parent # Let's redirect the user. print header, start_html, h2('Sending Mail...'), "\t<meta http-equiv=\"refresh\" content=\"5; url=http://host +.mydomain.com\">\n", end_html; $SIG{CHLD}='IGNORE'; exit; }else{ # $pid=0, so this is the child $retval=build_mail(); exit $retval; } }else{ # Oops, fork() returned undef - something is definitely wrong. DieNice("Unable to fork: $!\n"); } # Do your time consuming stuff, setting $retval to # a useful number sub build_mail { while (<@to>){ chomp($_); push @chunk, $_; $count ++; if ($count == 80){ #splits bcc into small chuncks $to = join ",", @chunk; mailout(); $count = 0; @chunk = (); } } $to = join ",", @chunk; mailout(); return $retval; }

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
•Re: Fork it!
by merlyn (Sage) on Nov 21, 2002 at 18:49 UTC