http://www.perlmonks.org?node_id=671984

Andre_br has asked for the wisdom of the Perl Monks concerning the following question:

Hello esteemed code fellows

I just can't make it work. When I comment out the 'sleep' call, it works, though - this proves the alarm is working.

But the alarm doesn't bother at all. The query always takes about 2 or 3 seconds, according to the cronometer I've set up. How come?

Here we go:

local our $dbh = DBI->connect ("DBI:mysql:database=$db:host=$mysqlhost +","user","pass") || die "Can't connect to database: $DBI::errst +r\n"; $dbh->{RaiseError} = 1; local our $sth = $dbh->prepare ( "SELECT this from that;"); # Start of cronometer local our $t0; local our $t1; local our $elapsed; $t0 = gettimeofday; ######################### # Start of timeout ##### eval { local $SIG{ALRM} = sub { die "Sorry, time's up.\n" }; alarm 1; # some operation that might take a long time to complete ######################### ######################### $sth->execute( ); # sleep 3; ######################### alarm 0; }; die $@ if $@; ######################### # End of timeout ######## # end of cronometer $t1 = gettimeofday; $elapsed = $t1 - $t0; print "Content-type: text/html\n\n"; print "Query took $elapsed seconds but the alarm didn't work!";
I'm using Perl 5.8.7, Win2K and mysql 4.1. And this is a mod_perl script.

Any ideas?

Thanks a lot

Andre

Replies are listed 'Best First'.
Re: Unable to setup timeout for DBI execute call
by renodino (Curate) on Mar 04, 2008 at 19:06 UTC
    Not certain why your method doesn't work, but have you tried DBIx::Timeout ?

    Perl Contrarian & SQL fanboy
      It doesn't work because of Perl's safe signals. The DBIx::Timeout module works around this effectively.
Re: Unable to setup timeout for DBI execute call
by markh (Scribe) on Mar 04, 2008 at 20:04 UTC
    Your mileage may vary, but I've never been able to get alarms to work on the Windows platform (Windows XP and server 2003 is what I've tried). I've used Perl 5.6.1 and 5.8.8 (both from Activestate) and no matter how simple the code is, the alarm never fires.
      Hello folks, thanks for the replies

      I've tested the DBIx::Timeout module and it adds serious processing overhead to the code. About 50 precious milisseconds, something we can't afford to add to all our searches.

      In fact, on Win2K the regular techniques described here on this link didn't work: http://search.cpan.org/~timb/DBI/DBI.pm#Signal_Handling_and_Canceling_Operations

      But, afterall, can I expect them to work on this $dbh call on my LINUX production server?

      Thanks