in reply to Creating a hash within a fork
In summary, one can take serial code and enable parallelism with little code.
open my $fh, $domains or die "CRIT: Unable to open $domains: $!\n"; my %domainList; while( my $domainName = <$fh> ) { chomp $domainName; my $expDate = `jwhois -n -h whois.crsnic.net $domainName | grep Ex +piration | awk '{print \$3}'`; my $diff = &dateDiff($expDate); if ($diff < 28 and $diff > 14) { $flag = 1; } elsif ($diff <= 14) { $flag = 2; } $domainList{$domainName} = $diff; } close $fh;
Parallelism is possible simply by wrapping MCE around serial code. This does not fork per each input item. Thus, graceful to the OS.
use MCE::Loop chunk_size => 1, max_workers => 5; my %domainList = mce_loop_f { my $domainName = $_; chomp $domainName; my $expDate = `jwhois -n -h whois.crsnic.net $domainName | grep Ex +piration | awk '{print \$3}'`; my $diff = &dateDiff($expDate); if ($diff < 28 and $diff > 14) { $flag = 1; } elsif ($diff <= 14) { $flag = 2; } MCE->gather($domainName, $diff); } $domains;
MCE can also take a file handle as input. However, this is less efficient for large files due to involving the manager process.
use MCE::Loop chunk_size => 1, max_workers => 5; open my $fh, $domains or die "CRIT: Unable to open $domains: $!\n"; my %domainList = mce_loop_f { my $domainName = $_; chomp $domainName; my $expDate = `jwhois -n -h whois.crsnic.net $domainName | grep Ex +piration | awk '{print \$3}'`; my $diff = &dateDiff($expDate); if ($diff < 28 and $diff > 14) { $flag = 1; } elsif ($diff <= 14) { $flag = 2; } MCE->gather($domainName, $diff); } $fh; close $fh;
|
|---|