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
In reply to How to timeout a while loop? by Andre_br
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |