On 11/22/05, Tom Allison <[EMAIL PROTECTED]> wrote:
> 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.
>

Tom,

Code isn't boring. Code is the only way for us to help you figure out
what's going on here. People who think otherwise don't read this list.

We can't help you debug a while loop if we can't see the while loop.
There's nothing in the code you just sent sent that does *anything*
five times.  That makes it kind of difficult to figure out why it's
not doing the *right* thing five times.  Give us something to work
with here. The code in your eval block looks fine; the problem is
either in the enclosing for/foreach/while/whatever block, or one of
your subroutines.

One other place you might want to look: to any of the subroutines or
modules set SIGALRM themselves or sleep for any period of time? See
perlipc in perldoc for why trying to set multiple alarms doesn't work,
and why mixing alarm and sleep timers can produce unexpected results.

Show us some more code, though, because what you're describing sounds
like an issue with the construction of the outer loop, or at least
that's the best guess from what you've provided so far.

-- jay
--------------------------------------------------
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.dpguru.com  http://www.engatiki.org

values of β will give rise to dom!

Reply via email to