http://www.perlmonks.org?node_id=998108

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

Hello Perl Monks

Could you please help me with what I am doing wrong

use Parallel::ForkManager; my $manager = new Parallel::ForkManager( 20 ); while (<LIST>){ $manager->start and next; my @array = split( /\s+/, $_ ); print "ip ? $array[1]\n"; unless ($p->ping($array[1])) {next; print "line\n"} etc... } print "flag\n"; $manager->finish(\&next); print "flag2 \n";

Everything works, but script prints out "flag" , but not "flag2". Please help me figure out how to go further. Thanks D

Replies are listed 'Best First'.
Re: Strange (for me) behavior of Parallel::ForkManager
by ikegami (Patriarch) on Oct 10, 2012 at 01:50 UTC

    $manager->finish(\&next); is exit(\&next). Aside from the fact that the argument makes no sense, you're exiting the child at the wrong spot. When that child is done processing a line, it needs to exit by calling finish.

    use Parallel::ForkManager; my $manager = new Parallel::ForkManager( 20 ); while (<LIST>){ $manager->start and next; my (..., $ip, ...) = split( /\s+/, $_ ); print "ip ? $ip\n"; $manager->finish() if !$p->ping($ip); ... $manager->finish(); } print "flag\n";
      And it still executes everything , but does not exit the loop...:(
Re: Strange (for me) behavior of Parallel::ForkManager
by mbethke (Hermit) on Oct 10, 2012 at 01:54 UTC

    That's expected behavior---finish() is not supposed to return. But why would you want to pass in a code reference as a return code? None of the example code I've looked at does that.

    In any case I what you need to do is call finish() within the loop:

    while (<LIST>){ $manager->start and next; my @array = split( /\s+/, $_ ); # do child stuff $manager->finish; }
      It works , but how to make it go out of the loop?
        There's only one process that loops. It loops until it reaches the end of the file, at which point it continues after the loop.
        If you need to leave early, you can use "last". Otherwise it will just read to EOF and then quit.