【发布时间】:2017-06-12 23:00:31
【问题描述】:
我的查询:
在下面的代码中,我试图将打印 $commandoutput[0] 转移或传递到即将到来的子程序中。我尝试转移来传递它。但我失败了。你能帮我正确的方法吗关注?
代码:
my $max_forks = 4;
#createThreads();
my %commandData;
my @arr = (
'bhappy', 'bload -m all -l -res CPUSTEAL',
'bqueues', 'bjobs -u all -l -hfreq 101'
);
#print @arr;
my $fork = new Parallel::ForkManager($max_forks);
$fork->run_on_start(
sub {
my $pid = shift;
}
);
$fork->run_on_finish(
sub {
my ( $pid, $exit, $ident, $signal, $core ) = @_;
if ($core) {
print "PID $pid core dumped.\n";
}
else { }
}
);
my @Commandoutput;
my $commandposition = 0;
for my $command (@arr) {
$fork->start and next;
my @var = split( " ", $command );
$commandoutput[$commandposition] = `$command`;
$commandposition++;
$line = $commandoutput[0];
# print $line;
$fork->finish;
}
$fork->wait_all_children;
#print Dumper(\%commandData);
print $commandoutput[0];
在这里,我尝试将打印 $commandoutput[0] 存储在子例程内的变量中。我在这里设置了如何将变量从外部传递到子例程内部。
sub gen_help_data
{
my $lines=shift;
print $lines;
}
【问题讨论】:
标签: perl multiprocessing fork ipc child-process