On 2012-07-30 15:47, punit jain wrote:

my $pm = new Parallel::ForkManager(10);

  my $count=0;

foreach my $user (@users) {

                $pm->start($user) and next;

                my $result;

              --- do some processing ---

                $pm->finish(0, \$result);

}



$pm->wait_all_children;


However the final value of count is not correct. Is there some race
condition on same variable updation by the processes ?

pm -> run_on_finish (

                sub {

                                my $result = @;

                                if (defined($result)) {

                                my $count += $result;

                                }

                }

I wonder why you expect a parent variable to be available to a child.
I am ignoring the bugs and style issues in your code, because they are not relevant to that point.

If you need to merge results from child-processes, it is best to let those child-processes write to unshared resources. Rows with an autoincrement-id in a database are often fine for that. A separate file per child-process on disk can also be fine.

After all children exited, the parent collects all those results and merges them.

Map: the parent divides the tasks over the children
Reduce: each child reduces its assigned input to a result
Merge: the parent combines all the results into the final result

--
Ruud


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to