John Doe wrote:
Tom Allison am Dienstag, 22. November 2005 12.24:

I figured out I can do this:

eval {
        local $SIG{ALRM} = sub { warn("alarm\n") };
        alarm 1;
        for(my $j = 0; $j < 1_000_000, $j++) {
                my $x = log(rand()*1000+1);
        }
        alarm 0;
};
if ( $@ ) {
        carp "[EMAIL PROTECTED]";
}

Which is very similar to the man pages.
But there are a few inconsistencies I'm trying to sort out.


The syntax is incorrect, see the comma in the for-().


I didn't copy it correctly, you are right about the syntax.
I also changed the warn to die.
I can get this to work in my test script.

Now, I've replaced the 'for(my $j = 0....}' loop with the real deal and it's just not working at all.

The real deal is more like this (I'm not posting all the code because it might be boring)

eval {
        local $SIG{ALRM} = sub { die "alarm\n" };
        alarm 60;
        my $ref = fetchdata($sql, $db);
        alarm 0'
};
....

and fetchdata() is a function in a module.
This function is basically a wrapper around DBI:

sub fetchdata {
    my ($sql, $db) = @_;
my $dbh = get_dbh($db) or die $@; # another function within this module
    my $ref = $dbh->fetchall_arrayref($sql) or die $@;
    return $ref;
}

----

I didn't write everything here perfectly so if there are syntax erros please forgive me. But I'm not sure how these ALRM signals behave with all these subroutine calls. I get one 'alarm' and then nothing, the sql runs for a long time. I'm intentially trying to test it to make sure it will 'alarm' more than once.. The problem is the database server gets overloaded so I want to try, wait, try, wait, try, die with each wait period being on the order of an hour. It would be convenient to do this if I could.

The alternative would be to run two shell scripts. The first to create a pidfile and the second to run only if that pidfile exists. Successful completion of the program would remove the pidfile.

But that's not as 'cool' or very perl-ish. I was hoping to be able to use the alarm.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to