tags:

views:

35

answers:

1

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 "After execute\n";
        $sth->finish;
    };
    alarm 0;
    die "$@" if $@;
};
die "$@" if $@;
print "Finished\n";
+1  A: 

Consider using Pg's asynchronous query feature instead.

ysth
Yeah, I also tried that. Unfortunately, there's no way to wait for the same query without also freezing at the same place. That is, unless I want to use a sleep loop, which adds unnecessary delays.
Flimzy