开发者

How can I get DBD::Pg to time out reliably?

Why doesn't this code execute the signal handler until after $sth->execute completes? And more importantly, how can I fix it?

#!/usr/bin/perl

use strict;
use warnings;

use DBI;
use Sys::SigAction qw( set_sig_handler );

my $dbh = DBI->connect('dbi:Pg:dbname=dc');

eval {
    my $h = set_sig_handler('ALRM', sub { die "timeout\n" });
    eval {
        alarm 1;
        my $sth = $dbh->prepare("SELECT pg_sleep(10)");
        print "Before execute\n";
        $sth->execute;
        print "Af开发者_运维百科ter execute\n";
        $sth->finish;
    };
    alarm 0;
    die "$@" if $@;
};
die "$@" if $@;
print "Finished\n";


Consider using Pg's asynchronous query feature instead.


Due to changes in the way Perl handles signals (so-called "safe signals" as of 5.8.0), you'll need to use Perl::Unsafe::Signals to allow your die() to work when $sth->execute is in progress.


There is AnyEvent::Pg that allows to query PostgreSQL asynchronously, though, it is not DBI compatible and it will force you to rewrite your app/script on top of AnyEvent.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜