Andre_br has asked for the wisdom of the Perl Monks concerning the following question:
I need to set a timeout for an operation I perform inside a while loop, but Iīm having trouble understanding the dinamic of the alarm() function. Can you guys help me out? Hereīs the (wrong!) code:
Thanks a lot fellows,$SIG{ALRM} = sub { print "...timeout"; next READING; }; my $number = 1; READING: while ( $number < 10 ) { eval { alarm(3); print $number; if ( $number == 3 ) { sleep 4; } # This is to simulate the delay + the routines may generate... $number++; print "\n"; } } if ($@) { # this I copied from the Camel book, but not sure about what + it means and if it is really necessary in this case... (I donīt want + to 'die'; i just want to go to the 'next' iteration.) if ($@ =~ /timeout/) { # timed out; do what you will here } else { alarm(0); # clear the still-pending alarm die; # propagate unexpected exception } }
Andre_br
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to timeout a while loop?
by suaveant (Parson) on Apr 25, 2005 at 20:13 UTC | |
by Andre_br (Pilgrim) on Apr 25, 2005 at 20:36 UTC | |
by suaveant (Parson) on Apr 25, 2005 at 20:47 UTC |