代码之家  ›  专栏  ›  技术社区  ›  Robert

Perl在web服务器上的最大执行时间

  •  1
  • Robert  · 技术社区  · 14 年前

    1 回复  |  直到 14 年前
        1
  •  3
  •   mfontani    14 年前

    如果您使用的是CGI(不是mod\u perl或FastCGI),那么在代码的顶部添加以下内容可能是值得的:

    alarm 30; # SIGALRM delivered to this process after 30 secs
    

    如果这个程序没有SIGALRM的处理程序,它就会死。

    例子:

    use strict;
    use warnings;
    alarm 2;
    my $n = 0;
    while ( 1 ) {
      print "$n\n";
      sleep 1;
      $n++;
    }
    __END__
    $ perl alr
    0
    1
    Alarm clock
    

    $SIG{ALRM} = sub {
      # do stuff
    };
    

    在使用闹钟()之前,您应该先放置该代码。

    例如,可以发送一封电子邮件通知您脚本花费的时间比预期的要长,或者其他什么。

    alarm 5;    # shouldn't take longer than 5 seconds. if not, die.
    do_stuff();
    alarm 0;    # reset the alarm
    

    希望这有帮助。