IO::Async::Timer::Countdown - event callback after a fixed delay
IO::Async::Timer::Countdown
use IO::Async::Timer::Countdown; use IO::Async::Loop; my $loop = IO::Async::Loop->new(); my $timer = IO::Async::Timer::Countdown->new( delay => 10, on_expire => sub { print "Sorry, your time's up\n"; $loop->loop_stop; }, ); $timer->start; $loop->add( $timer ); $loop->loop_forever;
This subclass of IO::Async::Timer implements one-shot fixed delays. The object implements a countdown timer, which invokes its callback after the given period from when it was started. After it has expired the Timer may be started again, when it will wait the same period then invoke the callback again. A timer that is currently running may be stopped or reset.
For a Timer object that repeatedly runs a callback at regular intervals, see instead IO::Async::Timer::Periodic. For a Timer that invokes its callback at a fixed time in the future, see IO::Async::Timer::Absolute.
Timer
The following events are invoked, either using subclass methods or CODE references in parameters:
Invoked when the timer expires.
The following named parameters may be passed to new or configure:
new
configure
CODE reference for the on_expire event.
on_expire
The delay in seconds after starting the timer until it expires. Cannot be changed if the timer is running.
Once constructed, the timer object will need to be added to the Loop before it will work. It will also need to be started by the start method.
Loop
start
If the timer is running, restart the countdown period from now. If the timer is not running, this method has no effect.
Because the reset method restarts a running countdown timer back to its full period, it can be used to implement a watchdog timer. This is a timer which will not expire provided the method is called at least as often as it is configured. If the method fails to be called, the timer will eventually expire and run its callback.
reset
For example, to expire an accepted connection after 30 seconds of inactivity:
... on_accept => sub { my ( $newclient ) = @_; my $stream; my $watchdog = IO::Async::Timer::Countdown->new( delay => 30, on_expire => sub { $stream->close }, ); $stream->add_child( $watchdog ); $stream = IO::Async::Stream->new( handle => $newclient, on_read => sub { my ( $self, $buffref, $closed ) = @_; $watchdog->reset; ... }, on_closed => sub { $watchdog->stop; }, ) ); $watchdog->start; $loop->add( $watchdog ); }
Paul Evans <leonerd@leonerd.org.uk>
To install IO::Async, copy and paste the appropriate command in to your terminal.
cpanm
cpanm IO::Async
CPAN shell
perl -MCPAN -e shell install IO::Async
For more information on module installation, please visit the detailed CPAN module installation guide.