Perl使用警报超时时出现分段错误 [英] Perl getting segmentation fault while timeout using alarm
问题描述
我是第一次写Perl,Internet告诉我,如果我的SQL查询运行时间很长,我可以使用Alarm。
我需要运行一个将在Perl中运行数小时的SP,我想设置一个时间限制。
下面是代码的最后一步,但是当进程运行的时间长于我的超时时间(此时是睡眠)时,我会遇到分段错误。日志显示它完成打印--- 1 ----然后出现分段错误。
如何解决此问题?
I'm writing Perl for the first time and Internet tells me i can use alarm if i have a long running sql query. I need to run a SP which will run for hours in a Perl, and i would like to set a time limit. Below is the last step of my codes but i'm getting segmentation fault when the process runs longer that my timeout, in this occasion sleep(20). Logs shows it finished printing ---1---- and then segmentation fault. How can I fix this?
我尝试将Sub DelRef {}取出到单独的.pl中,并在第三行中注释了$ db部分以进行测试如果警报运行正常,并且一切正常。我很困惑哪一部分出错了并导致了分段错误。
I tried taking sub DelRef{} out into a seperate .pl and comment the $db part in the third line to test if alarm is working fine, and it worked all right. I'm confused which part went wrong and caused the segmentation fault.
sub DelRef {
print "starting defRefData\n";
$db = new Sybapi($user, $password, $server, $margin_database);
print "entering eval\n";
my $timeout = 10;
eval {
local $SIG{ALRM} = sub { die "timeout\n" };
print "inside eval now\n";
alarm($timeout);
print "---1----\n";
#sleep(5); --working fine
sleep(20); #not working,
#$db->exec_sql("exec CPN_Margins..clean_up_refData_db '$XrefCode'");
print "----2----\n";
alarm(0);
};
#alarm(0);
print "out of eval\n";
if($@)
{
#die unless $@ eq "timeout\n";
if($@ =~ "timeout\n")
{
warn "Timed out!\n";
#exit 0;
}
else{
print $@;
}
}
}
&DelRef();
print "process is done\n";
$db->close();
推荐答案
来自perldoc( https://perldoc.perl.org/functions/alarm.html)-
From perldoc (https://perldoc.perl.org/functions/alarm.html)-
将警报和睡眠呼叫混在一起通常是一个错误,因为
睡眠可能在系统上通过警报实现。
It is usually a mistake to intermix alarm and sleep calls, because sleep may be internally implemented on your system with alarm.
如果要实现超时,可以不睡觉而实现。只需按照链接中提到的示例进行操作即可。
If you want to implement timeout, it can be achieved without sleep. Just follow the example mentioned in the link.
编辑:
我在这里添加了它-如何为Perl中长时间运行的Sybase sp设置超时时间
这篇关于Perl使用警报超时时出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!