Upon closer inspection, the manager process appears to be doing some heavy lifting inside the output iterator. The change below moves some of that to the worker process. This change frees the manager process from unnecessary overhead.
# MCE task to run in parallel
sub work {
my ($mce, $chunk_ref, $chunk_id) = @_;
my $data = $chunk_ref->[0];
my @ret = ();
foreach my $chunk (@$data) {
my %output = ();
foreach my $key (keys %$chunk) {
if ($key eq '.') {
$output{$key} = $$chunk{$key};
next;
}
my $val = $$chunk{$key};
my $uc = uc($key);
$val =~ s/$key/$uc/g;
$output{$key} = $val;
}
push(@ret,\%output);
}
my $buf = '';
foreach my $data (@ret) {
foreach my $key (sort keys %$data) {
$buf .= $$data{$key};
}
$buf .= "\n";
}
MCE->gather($chunk_id, $buf);
}
# make an output closure, returns an iterator
sub make_iter_output {
my ($path) = @_;
my %hold; my $order_id = 1;
open my $fh, '>', $path or die "open error: $!";
return sub {
my $chunk_id = shift;
# hold temporarily, until orderly
$hold{$chunk_id} = $_[0]; # $buf
while (1) {
last unless exists($hold{$order_id});
print {$fh} delete($hold{$order_id});
$order_id++;
}
};
}
Regards, Mario.