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

如何限制在Perl脚本的特定部分中花费的时间?

发布时间:2020-12-15 21:14:44 所属栏目:大数据 来源:网络整理
导读:有没有办法建立一个时间计数器,使脚本的部分运行只要它滴答?例如,我有以下代码: for my $i (0 .. $QUOTA-1) { build_dyna_file($i); comp_simu_exe; bin2txt2errormap($i);} 从理论上讲,我想运行这个循环3分钟,即使循环指令尚未完成,它仍然应该在3分钟后突
有没有办法建立一个时间计数器,使脚本的部分运行只要它滴答?例如,我有以下代码:
for my $i (0 .. $QUOTA-1) {
    build_dyna_file($i);
    comp_simu_exe;
    bin2txt2errormap($i);
}

从理论上讲,我想运行这个循环3分钟,即使循环指令尚未完成,它仍然应该在3分钟后突破循环.

实际上,程序打开一个时间计数器窗口,它与脚本的一部分并行工作(每次调用它).

此外,子调用’comp_simu_exe’运行外部模拟器(在shell中),当超时结束时 – 此过程也必须被杀死(不要假设在一段时间后返回).

sub comp_simu_exe{

system("simulator --shell");
}

系统函数调用之间是否有任何关联?

解决方法

这是第二个答案,它涉及超时第二个过程的情况.使用这种情况启动外部程序并确保它不会花太长时间:
my $timeout = 180;
my $pid = fork;

if ( defined $pid ) {
    if ( $pid ) {
        # this is the parent process
        local $SIG{ALRM} = sub { die "TIMEOUT" };
        alarm 180;
        # wait until child returns or timeout occurs
        eval {
            waitpid( $pid,0 );
        };
        alarm 0;

        if ( $@ && $@ =~ m/TIMEOUT/ ) {
            # timeout,kill the child process
            kill 9,$pid;
        }
    }
    else {
        # this is the child process
        # this call will never return. Note the use of exec instead of system
        exec "simulator --shell";
    }
}
else {
    die "Could not fork.";
}

(编辑:李大同)

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

    推荐文章
      热点阅读