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

Hi Fellow Monks! I'm trying to wring more performance from an e-mailing script, instead of sending messages serially via SMTP, I thought I'd run 4 child processes simultaneously sending e-mail via SMTP. The Parallel relevant code below works fine, the children fork, throughput increases, etc... the problem is that after running through the array and doing all the parallel execution, further code in the script doesn't execute!
my $pm = new Parallel::ForkManager(4); foreach my $bccperson (@bccppl) { $pm->run_on_start( sub { $scount++; print "Sending Msg:$mjobno:$scount:$bccperson\n"; } ); $pm->run_on_finish( sub { $xcount++; } ); $pm->start and next; my $msg = MIME::Lite->new( From => $from, To => $bccperson, Subject => $subject, Type => 'text', Data => $body ) || print "MIME::Lite new error: $bc +cperson\n"; MIME::Lite->send('smtp', '10.10.10.10', Timeout=>240); $msg->send() || print "Couldn't send to: $bccperson\n"; $pm->finish; } #End of foreach $bccperson $pm->wait_all_children; print "This Line prints Fine\n"; print "This line doesn't appear at all, no statements after the print + line above execute, how frustrating!!!\n";
Hope the robed ones can help!

Replies are listed 'Best First'.
Re: Parallel Forkmanager - Code not being executed?
by ehdonhon (Curate) on Jul 01, 2002 at 22:00 UTC

    This worked ok for me on 5.6.1. One observation, though:

    You should take the calls to run_on_start and run_on_finish and move them before the foreach loop. This way they are not being redefined with the same thing in each iteration.

Re: Parallel Forkmanager - Code not being executed?
by RMGir (Prior) on Jul 01, 2002 at 20:40 UTC
    Edit:Moved Windows question above the <readmore>

    Works here, with 5.6.0 on Linux, although without the actual emailing...

    Are you running this on Windows, perchance? I know fork on Windows is somewhat iffy; that could explain your problems...

    I don't know why mine would work and yours doesn't, but hopefully this helps a bit.

    Here's the trimmed down version of your code I used:

Re: Parallel Forkmanager - Code not being executed?
by yodabjorn (Monk) on Jul 02, 2002 at 00:58 UTC
    I highly reccomend using warnings(or -w) and strict

    This works on my Linux box.
    #!/usr/bin/perl -w use strict ; use Parallel::ForkManager ; use MIME::Lite ; my $scount = 0 ; my $xcount = 0 ; my $from = "Me"; my $subject = "Test"; my $body = "SOME test in here"; my @bccppl = ( 1..10 ) ; my $pm = new Parallel::ForkManager(4); $pm->run_on_start( sub { my ( $pid, $bccperson ) = @_ ; $scount++ ; print "Sending Msg: $scount $bccperson\n" ; } ); $pm->run_on_finish( sub { $xcount++; } ); foreach my $bccperson (@bccppl) { $pm->start($bccperson, $scount ) and next; my $msg = MIME::Lite->new( From => $from, To => $bccperson, Subject => $subject, Type => 'text', Data => $body ) or print "MIME::Lite new error: $bccperson\n"; MIME::Lite->send('smtp', '10.10.10.10', Timeout=>240); $msg->send() or print "Couldn't send to: $bccperson\n"; $pm->finish(); } #End of foreach $bccperson $pm->wait_all_children; print "Sent $xcount out of $scount msgs\n"; print "This Line prints Fine\n"; print "This line doesn't appear at all, no statements after the print +line above execute, how frustrating!!!\n";