Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Unable to setup timeout for DBI execute call

by Andre_br (Pilgrim)
on Mar 04, 2008 at 18:51 UTC ( #671984=perlquestion: print w/replies, xml ) Need Help??
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://671984]
Approved by kyle
help
Chatterbox?
[choroba]: Good morning!
[Corion]: I have a rough plan to release an ImageNet classifier in Perl (well, using pretrained data, but still a nice toy)
Discipulus is going mad with the new win10 client.. tried mv Cortana /dev/null but no succes
[Corion]: Ouch - another pretrained set is available, using images from IMDB and Wikipedia. So I expect interesting results for non-PR images where people are pictured that are not made up
[Corion]: Discipulus: Yeah, from my investigations, you can somewhat silence+disable Cortana, but some services of it remain always running unfortunately. What a waste of resources :-/
[Corion]: I hope you had a good weekend still ;)
choroba had a workshop with the band
[choroba]: which counts as a good weekend

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2017-09-25 08:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (277 votes). Check out past polls.

    Notices?