有时我的系统调用进入永无止境的状态。为了避免这种情况,我希望能够在指定的时间内打破呼叫。
有没有办法指定超时限制为system
?
system("command","arg1","arg2","arg3");
解决方法
请参阅
alarm
功能。来自pod的示例:
eval { local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required alarm $timeout; $nread = sysread SOCKET,$buffer,$size; alarm 0; }; if ($@) { die unless $@ eq "alarm\n"; # propagate unexpected errors # timed out } else { # didn't }
在CPAN上有一些模块可以更好地包装,例如:Time::Out
use Time::Out qw(timeout) ; timeout $nb_secs => sub { # your code goes were and will be interrupted if it runs # for more than $nb_secs seconds. }; if ($@){ # operation timed-out }
/ I3az /