1nickt has asked for the wisdom of the Perl Monks concerning the following question:
Hi all, I am processing a list of hashrefs with MCE::Loop and running into a problem when the list of hashrefs to be processed only contains one element. In this case it appears that MCE splits the hashref into its k=v pairs (how does it do that? how does it know it's a hashref?) and hands each one to a worker. Here's the test code:
and the output:use strict; use warnings; use feature 'say'; use Data::Dumper; ++$Data::Dumper::Sortkeys; use MCE::Loop; use MCE::Candy; my $aref = []; MCE::Loop::init( max_workers => 4, chunk_size => 1, gather => MCE::Candy::out_iter_array( $aref ), ); for ( 0, 1 ) { say "Test $_"; mce_loop { my ( $mce, $chunk_ref, $chunk_id ) = @_; warn "chunk_ref " . Dumper $chunk_ref; my ($chunk) = @{ $chunk_ref }; warn "chunk " . Dumper $chunk; MCE->gather( $chunk_id, $chunk->{'foo'} ); } @{ get_data( $_ ) }; say "$_ : " . Dumper $aref; $aref = []; } sub get_data { my $which = shift; if ( $which == 1 ) { return [ { foo => 'bar', baz => 'qux' } ]; } else { return [ { foo => 'bar', baz => 'qux' }, { foo => 'qux', baz => 'bar' }, ]; } } __END__
Test 0 chunk_ref $VAR1 = [ { 'baz' => 'qux', 'foo' => 'bar' } ]; chunk $VAR1 = { 'baz' => 'qux', 'foo' => 'bar' }; chunk_ref $VAR1 = [ { 'baz' => 'bar', 'foo' => 'qux' } ]; chunk $VAR1 = { 'baz' => 'bar', 'foo' => 'qux' }; 0 : $VAR1 = [ 'bar', 'qux' ]; Test 1 chunk_ref $VAR1 = { 'baz' => 'qux' }; Not an ARRAY reference at /tmp/mce.pl line 20, <__ANONIO__> line 3. chunk_ref $VAR1 = { 'foo' => 'bar' }; Not an ARRAY reference at /tmp/mce.pl line 20, <__ANONIO__> line 5. 1 : $VAR1 = [];
As you can see it works as expected even when there are fewer elements than workers ( as with two elements ). But it appears that with one element, MCE handles it differently. Thanks for any ideas!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: MCE::Loop with only one element in list
by marioroy (Prior) on Jun 01, 2017 at 04:10 UTC | |
by 1nickt (Canon) on Jun 01, 2017 at 04:19 UTC | |
by marioroy (Prior) on Jun 01, 2017 at 04:34 UTC |