So I have this Perl program reading a very large file, building hashes and keeping line number counters. The Ubuntu 20.04 system (on a 16 GB RAM and 4GB swap) stills kills the program because the hash eats the RAM. My workaround is use MCE to loop through the file chunks using 16 parallel workers.

The program builds the hashes and increments the counter alright but problem is the variables inside mce_loop_f{} are not available outside it so the rest of the code can not use the counter to run through the hash and print the hash vals to an output table. How can I get these variables and hashes be available?

I could not find in the many MCE docs a solution to achieve this except a mention of how Perl may re-spawn

It is possible that Perl may create a new code ref on subsequent runs causing MCE models to re-spawn. One solution to this is to declare global variables, referenced by workers, with "our" instead of "my".

use strict; use warnings; use MCE::Loop; use Data::Dumper; my %hash; our %hash2; our $counter1=0; our $counter2=0; open (my $fh, "<", "DATA_F") or die($!); print "printing counter1\n"; while(<$fh>){ my ($k, $v)=split; $hash{$k}=$v; print $counter1++,$/; } print "done printing counter1\n"; print "printing counter2\n"; MCE::Loop::init { use_slurpio => 1, max_workers => 16, init_relay=>0 }; mce_loop_f{ my ($mce, $file, $id)=@_; open (my $ifh, "<", $file) or die("$!"); while(<$ifh>){ my ($k, $v)=split; $hash2{$k}=$v; print $counter2++,$/; } } $fh; print "done printing counter2\n"; print $counter1, "=counter1 final\n"; print $counter2,"=counter2 final\n"; print Dumper(\%hash); print Dumper(\%hash2);
printing counter1 0 1 2 done printing counter1 printing counter2 0 1 2 done printing counter2 3=counter1 final 0=counter2 final #How can I get MCE to make $counter2 available to pri +nt()? $VAR1 = { '2' => 'two', '1' => 'one', '3' => 'three' }; $VAR1 = {}; #How can I get MCE to make %hash2 available to Dumper()?

Something or the other, a monk since 2009

In reply to MCE: How to access variables globally by biohisham

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.