my $maxProcesses=2;
my $pm = Parallel::ForkManager->new($maxProcesses, $tempDir);
# This has to be declared first.
$pm->run_on_finish(
sub {
my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_structure_reference) = @_;
my $reftype = ref($data_structure_reference);
$log->info(qq/PID:$pid,EXIT_CODE=$exit_code,IDENT=$ident,EXIT_SIGNAL=$exit_signal,CORE_DUMP=$core_dump,DATA=$data_structure_reference(REF_TYPE=$reftype)/);
if (defined($data_structure_reference)) { # children are not forced to send anything
my $string = ${$data_structure_reference}; # child passed a string reference
print "$string\n";
} else { # problems occuring during storage or retrieval will throw a warning
print qq|No message received from child process $pid!\n|;
}
} # End sub
);
####
use strict;
use XML::Smart;
use Parallel::ForkManager;
my $xmlin = XML::Smart->new($xmlInputFile); # Read in our input file defined in $xmlInputFile
# For the example posted here, I walk through the XML file where is the root node, and there are multiple 'row' elements containing various bits of data.
# I also use Log4perl to perform the logging, hence the the $log->info() calls.
foreach my $row (@{$xmlin->{'rows'}->{'row'}} ) {
# Fork our children...
$pm->start() and next;
# Grab the hostname.
my $host = $row->{HostName};
$log->info(qq/[$i] Checking host: $host / . ref($host));
# Do stuff...
$log->info(qq/[$i] Finished checking host: $host /);
$pm->finish(0, \$host );
}
####
2013-09-13_07:29:13 | INFO | main:: | [0] Checking host: a01719 XML::Smart
2013-09-13_07:29:13 | INFO | main:: | [0] Finished checking host: a01719
2013-09-13_07:29:13 | INFO | main::__ANON__ | PID:2166,EXIT_CODE=0,IDENT=,EXIT_SIGNAL=0,CORE_DUMP=0,DATA=(REF_TYPE=)
No message received from child process 2166!
2013-09-13_07:29:13 | INFO | main:: | [0] Checking host: a01719 XML::Smart
2013-09-13_07:29:13 | INFO | main:: | [0] Finished checking host: a01719
2013-09-13_07:29:13 | INFO | main::__ANON__ | PID:2167,EXIT_CODE=0,IDENT=,EXIT_SIGNAL=0,CORE_DUMP=0,DATA=(REF_TYPE=)
No message received from child process 2167!