如何在Perl中为长时间运行的Sybase SP设置超时

1 perl sybase timeout

我正在调用一个存储过程,该存储过程将从Perl中的Sybase DB中删除数据。但是sp需要几个小时才能完成。我只希望sp运行1个小时,然后无论它是否完成,我都希望以后的代码能够运行。我该如何实施?

sub DelRef {
    print "starting defRefData\n";
    $db = new Sybapi($user, $password, $server, $margin_database); 
    #the following sql will take hours
    $db->exec_sql("exec CPN_Margins..clean_up_refData_db '$XrefCode'");
}

&DelRef();
print "process is done\n";
$db->close();
Run Code Online (Sandbox Code Playgroud)

mob*_*mob 5

我总是对使用alarm中断系统调用保持警惕,因为我很难预测何时会忽略或恶化信号。

一种替代方法是在后台进程中运行长时间运行的代码,并在主进程中监视其进度。

# DelRef() might take a while ...
my $start_time = time;
my $pid = fork();
if ($pid == 0) {
    # child process
    DelRef();
    exit 0;
}
# parent process
while (1) {
    use POSIX qw( WNOHANG );
    my $waitpid = waitpid $pid, WNOHANG;
    if ($pid == $waitpid) {
        print STDERR "DelRef() finished successfully\n";
        last;
    }
    if (time - $start_time > 3600) {
        print STDERR "DelRef() didn't finish in an hour\n";
        kill 'TERM',$pid;    # optional
        last;
    }
    print STDERR "DelRef() is still running ...\n";
    sleep 60;
}
print STDERR "... the rest of the script ...\n";
Run Code Online (Sandbox Code Playgroud)