in reply to Perl Alarm Not Working

Still need more detail about &do_program(1, 100);. Is that (merely) a subroutine in the same script or is it a sub that calls an external program... or ??? IOW, show us the code of the sub.

Semi-irrelevant: The value in $@ is the error message from whatever you're calling in &do_program(1, 100);; it's not going to be populated the the case you describe.

Replies are listed 'Best First'.
Re^2: Perl Alarm Not Working
by aceofspace (Acolyte) on Dec 19, 2010 at 16:17 UTC
    &do_program(1, 100); is a subroutine in the same script.

    In a nutshell, calling the &do_program(1, 100), it will do the followings:

    1) Connect to the MySql database
    2) Retrive 100 domains beginning from row 1 for 100 rows in one of the tables.
    3) Remote access each of the 100 domains using LWP::UserAgent
    4) Then abstract the site(domain) page information

    It generally takes about 2 minutes to complete all of the above steps 1 to 4.
    (Notes: I've watched it ran many times, it took around that kind time to complete.)

    I'm mainly trying to find out if the scripts in the eval { } will work or not.

    They do not seem to work, because &do_program(1, 100) will continue to run from start to finish even though I set the alarm timeout variable, $timeout at different numbers less than 120(2 minutes), e.g. $timeout=20, $timeout=10, $timeout=5... down to 1 second $timeout=1.

    For some reason, there is no alarm signal generated in the eval {} block??
      As I said, your code works for me (in an eval block).

      One possibility is that MySQL is itself changing the signal handling. Mr. Google brought up a few suggestions: http://forums.mysql.com/read.php?51,256433,256478#msg-256478 is typical, with some alternative code, but I would have thought this would be common.

      The DBI provides the cancel operation on statement handles $sth->cancel() specifically for calling from alarm handlers. This implies that the database driver itself might be immune to SIGALRM and it is up to the Perl signal handler to cancel the operation.
        You have something there that MySql is immune to SIGALRM.

        Here's another test script I wrote to test SIGALRM in a script running MySql:

        #!/usr/bin/perl require 'myconfigure.cgi'; $timeout = 5; use DBI; $keywords = "Whether rocking natural-curls, a-short"; print "Content-type: text/html\n\n"; sub do_search { $time1 = time; $dbh=DBI->connect("dbi:mysql:$database:localhost","$username","$pa +ssword"); $keywords_quoted = $dbh ->quote ($keywords); $query="SELECT count(*) FROM $websites_table WHERE MATCH(title) AG +AINST ($keywords_quoted)"; $sth=$dbh->prepare(qq{$query}); $sth->execute(); $total_count = $sth->fetchrow_array (); $sth->finish; $dbh->disconnect ||die("Couldn't disconnect to database!\n"); $time2 = time; $time_ran = $time2 - $time1; } eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm $timeout; &do_search; alarm 0; }; if($@) { $time2 = time; $time_ran = $time2 - $time1; print "$@, Time Ran: $time_ran, Total Count: $total_count\n"; exit; } print "Time Ran: $time_ran, Total Count: $total_count\n"; exit;

        Above script basically do a search for the number of matches of $keywords in $websites_table. I've used a timeout of 5 seconds( $timeout = 5;) to limit the time of search, i.e. exit if search takes more than 5 seconds.

        When it timeouts by the eval{} block, it will indicate how much time the script has ran as shown by the variable $time_ran in the if($@) loop.

        But I'm getting funny results when it timeout. It overshoots the timeout limit of 5 sec condiderably. In one instance, I got a value of $time_ran = 18.

        How do I fix this problem?
      • "there is no alarm signal generated in the eval {} block?? "
        So, use debug or print statements to see if that's actually so... and, not just incidentally, time the sub at sub-second resolution (Time::HiRes, for example) or using (your favorite flavor of) profiler.
      • The pseudo-code in your points 1) .. 4) does NOT inform me of any possible issues inside the sub. Code, as requested (unless long) * would do so.

      * And if it's more than 10-15 lines, consider reducing it to a minimal form that still exhibits the problem. That serves two purposes: first, it may make something jump into view that clears up your question and, second, it makes it easier for us to help whilst our crystal balls are broken and psi blocked by DHS.

      My only guess is that the MySQL driver uses alarm internally, leaving it canceled.

      Can you simulate the DB code for testing purposes?