加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

异步运行perl的反引号

发布时间:2020-12-15 23:22:09 所属栏目:大数据 来源:网络整理
导读:现在我有一个perl脚本,在某个时刻,收集然后处理几个bash命令的输出,现在我就是这样做的: if ($condition) { @output = `$bashcommand`; @output1 = `$bashcommand1`; @output2 = `$bashcommand2`; @output3 = `$bashcommand3`;} 问题是,这些命令中的每一个
现在我有一个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;
}
...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读