16.21. Timing Out an OperationProblemYou want to make sure an operation doesn't take more than a certain amount of time. For instance, you're running filesystem backups and want to abort if it takes longer than an hour. Or, you want to schedule an event for the next hour. Solution
To interrupt a long-running operation, set a
SIGALRM handler to call $SIG{ALRM} = sub { die "timeout" }; eval { alarm(3600); # long-time operations here alarm(0); }; if ($@) { if ($@ =~ /timeout/) { # timed out; do what you will here } else { alarm(0); # clear the still-pending alarm die; # propagate unexpected exception } } Discussion
The
You cannot (usefully) give the See Also
The
"Signals"
sections in
Chapter 6
of
Programming Perl
and in
perlipc
(1); the
Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|