Re: updating variable in Parent using Parallel::ForkManager
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/
Re: updating variable in Parent using Parallel::ForkManager
punit jain wrote: Hi, Hello, I am facing an issue. Below is code snippet : - 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 ? Regards, Punit pm -> run_on_finish ( sub { my $result = @; if (defined($result)) { my $count += $result; Here you are creating a variable named $count which is only visible inside the scope of this subroutine, so your other $count variable is not affected. } } John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: updating variable in Parent using Parallel::ForkManager
Hi Punit, On Mon, 30 Jul 2012 19:17:21 +0530 punit jain wrote: > Hi, > > > I am facing an issue. Below is code snippet : - > It is a good idea to include a self-contained reproducing code that will demonstrate the problem - not incomplete snippets. > 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 ? > > Regards, > Punit > > > > pm -> run_on_finish ( > >sub { > >my $result = @; > >if (defined($result)) { > >my $count += $result; > This is wrong - $count will be a lexical variable only present inside its scope. You should declare it global to the scope - outside the anonymous subroutine: <<< my $count = 0; $pm->run_on_finish( sub { $count += $result; } ); >>> For more information see: http://perl.plover.com/FAQs/Namespaces.html Regards, Shlomi Fish -- - Shlomi Fish http://www.shlomifish.org/ Freecell Solver - http://fc-solve.shlomifish.org/ I’d do Windows-- , but this may result in an integer underflow. — an Israeli Linuxer. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/