2011-07-10 57 views
4

我正在使用Parallel :: ForkManager將一些變量從子項傳遞給父項。我能夠通過$ var ...我怎麼傳遞更多? (再次感謝霍布斯幫助我走到這一步)parallel :: forkmanager ....如何在出口傳遞2個變量

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)[email protected]_; 
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; 

回答

4

只能有一個:)

傳遞一個數組引用:

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

或哈希參考:

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

And:

$pm->run_on_finish(sub{ 
    my ($pid,$exit_code,$ident,$exit_signal,$core_dump,$data)[email protected]_; 
    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)[email protected]_; 
    print "pid: ".$pid."\n"; 
    print " first: ".$data->{'var'}."\n"; 
    print " second: ".$data->{'v'}."\n"; 
}); 
+0

我如何再從run_on_finish子程序訪問的變量? –

+0

@ user522962,如果刪除所有不需要的「\」,可以使用'my $ resp = $ _ [5]來訪問*值*。我的($ var,$ v)= @ $ resp; ... $ var ... $ v ...'和'my $ resp = $ _ [5]; ... $ resp - > {var} ... $ resp - > {v} ...'分別。 – ikegami

+1

@ysth,你不應該創建對標量的引用。 OP做到了,因爲Storable需要一個引用,但''''和'{}'已經返回一個引用。 – ikegami

相關問題