现在我有一个perl脚本,在某个时刻,收集然后处理几个bash命令的输出,现在我就是这样做的:
if ($condition) { @output = `$bashcommand`; @output1 = `$bashcommand1`; @output2 = `$bashcommand2`; @output3 = `$bashcommand3`; }
问题是,这些命令中的每一个都需要相当长的时间,因此,我想知道我是否可以同时运行它们.
解决方法
这听起来像
Forks::Super::bg_qx
的一个很好的用例.
use Forks::Super 'bg_qx'; $output = bg_qx $bashcommand; $output1 = bg_qx $bashcommand1; $output2 = bg_qx $bashcommand2; $output3 = bg_qx $bashcommand3;
将在后台运行这四个命令.用于返回值的变量($output,$output1等)是重载对象.您的程序将在下次在程序中引用这些变量时检索这些命令的输出(等待命令完成,如有必要).
... more stuff happens ... # if $bashcommand is done,this next line will execute right away # otherwise,it will wait until $bashcommand finishes ... print "Output of first command was ",$output; &do_something_with_command_output( $output1 ); @output2 = split /\n/,$output2; ...
更新2012-03-01:vs.60 of Forks :: Super has some new constructions,可让您在列表上下文中检索结果:
if ($condition) { tie @output,'Forks::Super::bg_qx',$bashcommand; tie @output1,$bashcommand1; tie @output2,$bashcommand2; tie @output3,$bashcommand3; } ...