Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Strange (for me) behavior of Parallel::ForkManager

by Sterh (Novice)
on Oct 10, 2012 at 01:39 UTC ( #998108=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on Strange (for me) behavior of Parallel::ForkManager
Download Code
Re: Strange (for me) behavior of Parallel::ForkManager
by ikegami (Pope) 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?
        If you need to leave early, you can use "last". Otherwise it will just read to EOF and then quit.
        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://998108]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (14)
As of 2014-10-24 13:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (132 votes), past polls