forking and ending a CGI process

Discussion in 'Perl Misc' started by Rob Young, Aug 11, 2003.

  1. Rob Young

    Rob Young Guest

    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
     
    Rob Young, Aug 11, 2003
    #1
    1. Advertising

  2. "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
     
    Gregory Toomey, Aug 11, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roy Smith

    Forking sub-process in CppUnit?

    Roy Smith, Jul 4, 2003, in forum: C++
    Replies:
    0
    Views:
    385
    Roy Smith
    Jul 4, 2003
  2. Robin Munn

    Re: Bug help: CGI forking

    Robin Munn, Aug 1, 2003, in forum: Python
    Replies:
    0
    Views:
    549
    Robin Munn
    Aug 1, 2003
  3. LarsenMTL
    Replies:
    4
    Views:
    758
    Eric Walstad
    Nov 4, 2004
  4. Andrew Robert

    Process forking on Windows

    Andrew Robert, May 17, 2006, in forum: Python
    Replies:
    9
    Views:
    2,191
    Benji York
    May 19, 2006
  5. Clyde Ingram
    Replies:
    1
    Views:
    457
    Randal L. Schwartz
    Dec 12, 2003
Loading...

Share This Page