怎么能超时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); }