gpt4 book ai didi

Perl,在 x 秒后使脚本超时?

转载 作者:行者123 更新时间:2023-12-04 22:08:31 24 4
gpt4 key购买 nike

我一直在搜索这个问题,但很难直接回答这个问题(因为 php 似乎有更多关于这个主题的信息)。我需要让我的 perl 脚本在指定的秒数后死亡,因为,就像现在一样,它们运行时间太长并堵塞了我的系统,我该如何做到让整个脚本在指定的秒数后消失?

我知道杀死脚本的外部解决方案,但我想从 perl 脚本本身内部完成。

谢谢

最佳答案

perldoc -f alarm :

[sinan@kas ~]$ cat t.pl
#!/usr/bin/perl

use strict; use warnings;

use Try::Tiny;

try {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm 5;
main();
alarm 0;
}
catch {
die $_ unless $_ eq "alarm\n";
print "timed out\n";
};

print "done\n";

sub main {
sleep 20;
}

输出:

[sinan@kas ~]$ time perl t.pl
时间到
完毕

真正的 0m5.029s
用户 0m0.027s
系统 0m0.000s

关于Perl,在 x 秒后使脚本超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3427401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com