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

Hi, I have a problem with the Parallel::ForkManager module. I tried for a CGI in a subroutine
sub whatEver(){ my $list = shift; #eg {'hammer' => "nail"} my %result = (); my $pm = new Parallel::ForkManager(10); foreach my $query (keys %{$list}) { $pm->start and next; $result{$query} = 1; print Dumper("1", \%result); $pm->finish(); # do the exit in the child process } $pm->wait_all_children; print Dumper("2", \%result); }
$VAR1 = {1}; $VAR2= { 'hammer' => 1 }; $VAR1 = {2}; $VAR2 = {}; within the foreach loop %result is defined, outsite not!

Replies are listed 'Best First'.
Re: Parallel::ForkManager problem
by tirwhan (Abbot) on Mar 28, 2006 at 09:56 UTC

    The call to $pm->start creates a new process with it's own memory address space, variables are not shared between processes. So once you have 10 processes started you have 11 versions of the hash %result in memory, each of them addressable only from the process it belongs to. The one you access outside the loop belongs to the parent process and is never modified.

    If you want to use a shared variable you may want to consider using threads instead of processes. Alternatively, use one of the forms of interprocess communication described in perldoc perlipc or search CPAN for "IPC" or "shared".


    All dogma is stupid.
      in the OP example, using fork is still a valid option because the exit code can be used to pass an integer result back to the parent.

        Yeah,though to me that has always seemed a bit of a hackish way of doing things. Exit codes should communicate error conditions or success of a process, if you want to pass regular information between child and parents use IPC (it's not like that's really so much more difficult than setting up the exit code handler). YMMV of course.


        All dogma is stupid.
Re: Parallel::ForkManager problem
by salva (Canon) on Mar 28, 2006 at 11:08 UTC
    if all you need is passing a boolean ok value from the child to the parent, in Parallel::ForkManager you can use the run_on_finish callback to get the exit code of the child.

    As I am the author of Proc::Queue let me use that module instead...

    use Proc::Queue size => 10; sub whatEver { my $list = shift; my (%pid, %result); foreach my $query (keys %$list) { my $pid = fork; defined $pid or die "fork failed"; if (!$pid) { my $ok = do_whatever(); exit (!$ok); } else { $pid{$query} = $pid; } } for my $query (keys %pid) { if (waitpid($pid{$query}, 0) > 0) { $result{$query} = ! $? } } print Dumper("2", \%result); }
      $result{$query} = ! $? can you explain this! Please
        Murcia,
        If you look in perlvar, you will see that $? is the status returned by the last closed pipe. The ! is a way to booleanize it. If it has a non-zero value, it becomes 0 and if it had a 0 value, it becomes 1.

        Cheers - L~R