如何指定 Perl 系统调用的超时限制?

发布于 2024-09-28 06:23:15 字数 296 浏览 0 评论 0原文

有时我的系统调用会进入永无止境的状态。为了避免我希望能够在指定的时间后中断通话。

有没有办法指定 system 的超时限制?

system("command", "arg1", "arg2", "arg3");

我希望从 Perl 代码中实现超时以实现可移植性,而不是使用 ulimit 等某些操作系统特定的函数。

Sometimes my system call goes into a never ending state. To, avoid that I want to be able to break out of the call after a specified amount of time.

Is there a way to specify a timeout limit to system?

system("command", "arg1", "arg2", "arg3");

I want the timeout to be implemented from within Perl code for portability, and not using some OS specific functions like ulimit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

东走西顾 2024-10-05 06:23:15

请参阅 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 上有一些模块可以更好地包装这些模块,例如: 时间::输出

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
}

See the alarm function. Example from 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
}

There are modules on CPAN which wrap these up a bit more nicely, for eg: 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
}
忆离笙 2024-10-05 06:23:15

您可以使用 IPC::Run 的 run 方法代替系统。并设置一个超时时间。

You can use IPC::Run's run method instead of system. and set a timeout.

恋你朝朝暮暮 2024-10-05 06:23:15

我之前刚刚在 Perl + Linux 中使用过 timeout 命令,您可以这样测试:

for(0..4){
  my $command="sleep $_";  #your command
  print "$command, ";
  system("timeout 1.1s $command");  # kill after 1.1 seconds
  if   ($? == -1  ){ printf "failed to execute: $!" }
  elsif($?&127    ){ printf "died, signal %d, %scoredump", $?&127, $?&128?'':'no '}
  elsif($?>>8==124){ printf "timed out" }
  else             { printf "child finished, exit value %d", $? >> 8 }
  print "\n";
}

4.317 秒后输出:

sleep 0, child finished, exit value 0
sleep 1, child finished, exit value 0
sleep 2, timed out
sleep 3, timed out
sleep 4, timed out

timeout 命令是所有主要“正常”命令的一部分“ Linux 发行版是 coreutils 的一部分。

I've just used the timeout command in Perl + Linux before, something you can test like this:

for(0..4){
  my $command="sleep $_";  #your command
  print "$command, ";
  system("timeout 1.1s $command");  # kill after 1.1 seconds
  if   ($? == -1  ){ printf "failed to execute: $!" }
  elsif($?&127    ){ printf "died, signal %d, %scoredump", $?&127, $?&128?'':'no '}
  elsif($?>>8==124){ printf "timed out" }
  else             { printf "child finished, exit value %d", $? >> 8 }
  print "\n";
}

Output after 4.317 seconds:

sleep 0, child finished, exit value 0
sleep 1, child finished, exit value 0
sleep 2, timed out
sleep 3, timed out
sleep 4, timed out

The timeout command is part of all major "normal" Linux distributions a.f.a.i.k, a part of coreutils.

说谎友 2024-10-05 06:23:15

System::Timeout 怎么样?

此模块扩展了系统以允许在指定秒数后超时。

timeout("3", "sleep 9"); # timeout exit after 3 seconds

How about System::Timeout ?

This module extends system to allow timeout after the specified seconds.

timeout("3", "sleep 9"); # timeout exit after 3 seconds
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文