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

如何在perl中“可靠地”计算300秒的时间?

发布时间:2020-12-16 06:18:02 所属栏目:大数据 来源:网络整理
导读:怎么能超时5分钟? 我的程序是这样做的: # I need to try something every 3 seconds,for at most 5 minutes$maxtime = time() + (5 * 60);$success = 0;while (($success == 0) (time() $maxtime)) { $success = try_something(); sleep (3) if ($success
怎么能超时5分钟?

我的程序是这样做的:

# I need to try something every 3 seconds,for at most 5 minutes
$maxtime = time() + (5 * 60);
$success = 0;
while (($success == 0) && (time() < $maxtime)) {
  $success = try_something();
  sleep (3) if ($success == 0);
}

问题:这个程序在启动后运行.它运行的嵌入式系统没有rtc / clock电池.时钟从2000年1月1日开始,然后在它运行的第一分钟,它获得网络并且ntp将时钟设置为更新的时钟,使循环在5分钟超时之前退出.

哪个是在perl脚本中“计算5分钟”的正确方法,即使系统时钟被其他外部程序更改了?

解决方法

我认为使用闹钟功能在这里是有道理的.

{
  local $SIG{ALRM} = sub {
     warn "Ooops! timed out,exiting";
     exit(100); # give whatever exit code you want
  };

  ## setup alaram
  alarm( 5 * 60 );
  my $success = 0;
  until($success) {
    $success = try_something()
       or sleep 3;
  }

  ## deactivate alarm if successful
  alarm(0);
}

(编辑:李大同)

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

    推荐文章
      热点阅读