Perl MCE returns hash data to the main process

I am trying to run the Perl Many-Core Engine, which works fine. But when the worker adds data to the global hash in the subroutine, this data is lost after the MCE process is completed (see comment position below). I did not think this would be a problem since the% data is global. Any advice is greatly appreciated.

#!/usr/bin/perl
use strict;
use warnings;
use MCE;

my @symbol = ('abc','def');
my $CPUs = 1;
my %data = ();

process();

sub process {
    my $mce = MCE->new(
        input_data  => \@symbol,
        max_workers => $CPUs,
        user_func => sub {
            my ($self, $sym, $chunk_id) = @_;
            $sym = $sym->[0];
            doTask($sym);
        }
    );
    $mce->run();

    print %data; # if I check contents of %data at this line in the code, its empty
}

sub doTask {
    my ($sym) = @_;
    $data{$sym} = 1;

    print %data;
    print "\n\n";

    return;
}
+4
source share
4 answers

It works fine using the MCE :: Map model, which has a built-in data collection function.

#!/usr/bin/perl
use strict;
use warnings;
use MCE::Map;

my @symbol = ('abc','def');
my $CPUs = 1;
my %data = ();

process();

sub process {
    MCE::Map::init {chunk_size => 1, max_workers => $CPUs};
    %data = mce_map {doTask($_)} @symbol;
    print %data; # data exchange between the manager and worker processes works
}

sub doTask {
    my ($sym) = @_;
    $data{$sym} = 1;
    return %data;
}
+1
source

Full data sharing capabilities that support deep sharing will be included in the upcoming version of MCE 1.7.

use MCE;
use MCE::Shared;

mce_share my %data;

, .

+1

doTask? user_func user_tasks:

my $mce = MCE->new(
    input_data  => \@symbol,
    user_tasks  => [{
        max_workers => $CPUs,
        user_func   => sub {
            my ($self, $sym, $chunk_id) = @_;
            $sym = $sym->[0];
            doTask($sym);
    }]
);
$mce->run();
0

, -, MCE foreach MCE:: Loop, . MCE:: Map. - :: share, , http://www.perlmonks.org/?node_id=798735, shared_clone .

# the following lines cannot be ommitted otherwise error "Invalid value for shared scalar"
unless (exists $HoH{$F[0]})
{
my %p : shared;
$HoH{$F[0]} = \%p;
}
# the following lines if skipped will randomly missing a few records
unless (exists $HoH{$F[0]}{$mf})
{
    my @c : shared;
    $HoH{$F[0]}{$mf} = \@c;
}  
$HoH{$F[0]}{$mf}=shared_clone([$F[3],$F[4]]);
#we cannot use $HoH{$mf}=shared_clone(\%Loss) directly, where %Loss is a hash of arrary

BTW, script, perl, " ", " threads:: shared", .

-1

Source: https://habr.com/ru/post/1570555/


All Articles