parallel :: forkmanager ....如何在退出时传递2个变量

时间:2011-07-10 18:44:21

标签: perl fork pipe

我使用Parallel :: ForkManager将一些变量从子节点传递给父节点。我能通过$ var ...我怎么通过更多? (再次感谢Hobbs帮助我做到这一点)

use Parallel::ForkManager;

my $pm=new Parallel::ForkManager(10); 
my(@cusips,@bad);
$pm->run_on_finish(sub{
my($pid,$exit_code,$ident,$exit_signal,$core_dump,$var,$v)=@_;
print "pid: ".$pid."\n";
print " first: ".${$var}."\n"; 
print " second: ".${$v}."\n";   
});


for(1..3){

$pm->start and next; # do the fork 
my $var=rand();
my $v=rand();

$pm->finish(0,\$var,\$v); # do the exit in the child process 
}


$pm->wait_all_children; 

1 个答案:

答案 0 :(得分:4)

只能有一个:)

传递数组引用:

$pm->finish(0, [ $var, $v ]);

或哈希引用:

$pm->finish(0, { 'var' => $var, 'v' => $v });

$pm->run_on_finish(sub{
    my ($pid,$exit_code,$ident,$exit_signal,$core_dump,$data)=@_;
    print "pid: ".$pid."\n";
    print " first: ".$data->[0]."\n"; 
    print " second: ".$data->[1]."\n";   
});

$pm->run_on_finish(sub{
    my ($pid,$exit_code,$ident,$exit_signal,$core_dump,$data)=@_;
    print "pid: ".$pid."\n";
    print " first: ".$data->{'var'}."\n"; 
    print " second: ".$data->{'v'}."\n";   
});