Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > forking and ending a CGI process

Reply
Thread Tools

forking and ending a CGI process

 
 
Rob Young
Guest
Posts: n/a
 
      08-11-2003
Hi;
I spent about 4 hours searching through newsgroups, faqs, and O'reilly
books with various odd-smelling desert animals on the cover. I found
that this question has been asked A LOT, but none of the answers worked
for me. I'm posting this in hopes it will help someone else, and to
solicit comments on why the other methods did not work and whether this
is the best way.

The problem: You are batching e-mails to a large number of subscribers,
and you don't want the CGI script to wait until the mail process
finishes before completing the page.

Many people wrote to just put
$|=1;
at the top of your script. I am using the Mail::Bulkmail module, so
I tried this down near the end of the routine after the $bulk object had
been defined.

$|=1;
print "";
print qq(Thank you.... );
$bulk->bulkmail() or die $bulk->error;

and the script hesitated for 17 seconds before printing the thankyou
statement, just as it did without the buffer change. Mail was delivered OK.

Then I tried forking it like this:

if (my $pid = fork) {
print qq(Thank you.... );
} else {
die "cannot fork: $!" unless defined $pid;
$bulk->bulkmail() or die $bulk->error;
}

No difference.

Then I tried closing STDOUT

$|=1;
print "";
if (my $pid = fork) {
print qq(Thank you.... );
close(STDOUT);close(STDERR);
} else {
die "cannot fork: $!" unless defined $pid;
$bulk->bulkmail() or die $bulk->error;
}

That got instant results, but no mail was sent. I'm guessing
that Apache terminated the process when STDOUT closed?

Some people wrote to write the entire script to a new file and
then use system() to call it, but this script needs to be portable,
and some CGI wrappers don't allow system() calls or backticks.

This is what finally worked:

$|=1;
print "";
if (my $pid = fork) {
print qq(Thank you.... );
kill("KILLBUFFER"=>$$);
} else {
die "cannot fork: $!" unless defined $pid;
$bulk->bulkmail() or die $bulk->error;
}

Comments or suggestions welcome.

Rob






 
Reply With Quote
 
 
 
 
Gregory Toomey
Guest
Posts: n/a
 
      08-11-2003
"Rob Young" <> wrote in message
news:Z2RZa.340165$ ...
> Hi;
> I spent about 4 hours searching through newsgroups, faqs, and O'reilly
> books with various odd-smelling desert animals on the cover. I found
> that this question has been asked A LOT, but none of the answers worked
> for me. I'm posting this in hopes it will help someone else, and to
> solicit comments on why the other methods did not work and whether this
> is the best way.
>
> The problem: You are batching e-mails to a large number of subscribers,
> and you don't want the CGI script to wait until the mail process
> finishes before completing the page.


Use the "daemonzie" routine
http://www.perldoc.com/perl5.8.0/pod/perlipc.html


gtoomey


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Process forking on Windows Andrew Robert Python 9 05-19-2006 08:26 PM
Python CGI - Accepting Input, Invoking Another Process, Ending CGI LarsenMTL Python 4 11-04-2004 05:59 PM
Forking a daemonic Socket listener from a CGI script - browser times out Clyde Ingram Perl Misc 1 12-12-2003 04:11 PM
Re: Bug help: CGI forking Robin Munn Python 0 08-01-2003 07:27 PM
Forking sub-process in CppUnit? Roy Smith C++ 0 07-04-2003 12:58 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57