gravid, welcome to the monastery.

The following provide 4 demonstrations. MCE modules support Perl not built with threads support. The forth demonstration ensures graceful IO from shared storage or from a network-based file system. In that case, MCE does sequential IO among workers. Parallel IO is possible by adding the MCE option: parallel_io => 1, to have workers read simultaneously (random IO).

threads and threads::shared

use strict; use warnings; use threads; use threads::shared; my @dirs = qw( a b c d e f ); my %hash = map { $_ => shared_clone({}) } @dirs; my @thrs; foreach my $dir (@dirs) { if (-e "$dir/syn.log") { push @thrs, threads->create(\&process_1_file, $dir, "syn.log"); } } $_->join for @thrs; print $hash{"a"}{"area"}, "\n"; sub process_1_file { my ($dir, $file) = @_; open(my $fh, "$dir/$file") or die "cannot open $dir/$file $!"; while (<$fh>) { chomp; if ($_ =~ /^area=(.*)/) { $hash{"$dir"}{"area"} = $1; } } }

MCE::Hobo and MCE::Shared

use strict; use warnings; use MCE::Hobo; use MCE::Shared; my @dirs = qw( a b c d e f ); my %hash = map { $_ => MCE::Shared->hash } @dirs; foreach my $dir (@dirs) { if (-e "$dir/syn.log") { MCE::Hobo->create(\&process_1_file, $dir, "syn.log"); } } MCE::Hobo->waitall; print $hash{"a"}{"area"}, "\n"; sub process_1_file { my ($dir, $file) = @_; open(my $fh, "$dir/$file") or die "cannot open $dir/$file $!"; while (<$fh>) { chomp; if ($_ =~ /^area=(.*)/) { $hash{"$dir"}{"area"} = $1; } } }

MCE::Loop and MCE::Shared

use strict; use warnings; use MCE::Loop; use MCE::Shared; my @dirs = qw( a b c d e f ); my %hash = map { $_ => MCE::Shared->hash } @dirs; MCE::Loop->init( max_workers => $#dirs, chunk_size => 1 ); mce_loop { my $dir = $_; proccess_1_file($dir, "syn.log") if (-e "$dir/syn.log"); } @dirs; print $hash{"a"}{"area"}, "\n"; sub proccess_1_file { my ($dir, $file) = @_; open(my $fh, "$dir/$file") or die "cannot open $dir/$file $!"; while (<$fh>) { chomp; if ($_ =~ /^area=(.*)/) { $hash{"$dir"}{"area"} = $1; } } }

MCE and MCE::Shared

use strict; use warnings; use MCE; use MCE::Shared; my @dirs = qw( a b c d e f ); my %hash = map { $_ => MCE::Shared->hash } @dirs; my $mce = MCE->new( max_workers => 4, chunk_size => '12m', use_slurpio => 1, user_func => sub { my ($mce, $slurp_ref, $chunk_id) = @_; my $dir = $mce->user_args()->[0]; open my $MEM_FH, "<", $slurp_ref; while (<$MEM_FH>) { chomp; if ($_ =~ /^area=(.*)/) { $hash{"$dir"}{"area"} = $1; } } close $MEM_FH; } )->spawn; foreach my $dir (@dirs) { if (-e "$dir/syn.log") { $mce->process({ user_args => [ $dir ] }, "$dir/syn.log"); } } $mce->shutdown; print $hash{"a"}{"area"}, "\n";

Warm regards. Mario


In reply to Re: hashes & threads by marioroy
in thread hashes & threads by gravid

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.