develooper Front page | perl.beginners | Postings from August 2012

Re: updating variable in Parent using Parallel::ForkManager

Thread Previous | Thread Next
From:
Dr.Ruud
Date:
August 1, 2012 14:35
Subject:
Re: updating variable in Parent using Parallel::ForkManager
Message ID:
20120801213507.11369.qmail@lists-nntp.develooper.com
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


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About