The MCE::Shared module provides data sharing capabilities. It works reliably with Parallel::ForkManager on Unix platforms. The following is a demonstration via the TIE interface. It is based on the OP's code with slight modification.

use strict; use warnings; use Parallel::ForkManager; use MCE::Shared; my $pm = Parallel::ForkManager->new(3); my @group = ( [ 'f1','f2','f3' ], [ 'f4','f5','f6' ], [ 'f7','f8','f9' ] ); tie my @quality, 'MCE::Shared'; foreach my $i (@group) { $pm->start and next; foreach my $term (@$i) { warn "No such file. - $term\n" and next if ! -e $term; ... my $IsGood = ...; push @quality, $IsGood; } $pm->finish; } $pm->wait_all_children; print join("\n", @quality), "\n";


The OO interface to MCE::Shared is TIE'less for faster execution.

use strict; use warnings; use Parallel::ForkManager; use MCE::Shared; my $pm = Parallel::ForkManager->new(3); my @group = ( [ 'f1','f2','f3' ], [ 'f4','f5','f6' ], [ 'f7','f8','f9' ] ); my $quality = MCE::Shared->array; foreach my $i (@group) { $pm->start and next; foreach my $term (@$i) { warn "No such file. - $term\n" and next if ! -e $term; ... my $IsGood = ...; $quality->push($IsGood); } $pm->finish; } $pm->wait_all_children; print join("\n", $quality->vals), "\n";


Finally, the same thing using MCE::Loop. Due to the nested structure of the array, @group must be back-slashed when passing into mce_loop.

use strict; use warnings; use MCE::Loop max_workers => 3, chunk_size => 1; use MCE::Shared; my @group = ( [ 'f1','f2','f3' ], [ 'f4','f5','f6' ], [ 'f7','f8','f9' ] ); my $quality = MCE::Shared->array; mce_loop { my $i = $_; foreach my $term (@$i) { warn "No such file. - $term\n" and next if ! -e $term; ... my $IsGood = ...; $quality->push($IsGood); } } \@group; print join("\n", $quality->vals), "\n";


Warm regards, Mario.


In reply to Re^3: shared array while use ForkManager by marioroy
in thread shared array while use ForkManager by mlin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.