mojolicious - Start stop reset timer in perl -


is there start/stop timer in perl. had tried anyevent 1 time or recurring timer. once set, can reset timeout interval.

i have requirement have reset timer if event occurs within timer timeout interval. there perl module job?

thanks in advance.

update

this question prompted quite bit of discussion on #mojo irc channel. end result that, barring unforseen problems, upcoming mojolicious 4.0 release include new reactor method again can restart timers. turns out new method (inspired partially question) provides massive performance increase when used internally mojolicious in case (high load high concurrency). once 4.0 released, try updated example version of second example below:

#!/usr/bin/env perl  use mojo::base -strict; use mojo::ioloop;  $loop = mojo::ioloop->singleton;  $now = 1; $loop->recurring( 1 => sub { print $now++ . "\n" } );  $timer = $loop->timer( 3 => \&boom );  $loop->timer( 2 => sub {    print "event resets. no boom yet\n";   $loop->reactor->again($timer); });  $loop->start;  sub boom {    print "boom!\n";   $loop->stop; } 

original

here quick , dirty using mojo::ioloop directly. if run inside server don't need start , stop methods. there countdown variable may reset elsewhere , recurring timer checks see if countdown has expired before goes boom.

#!/usr/bin/env perl  use mojo::base -strict; use mojo::ioloop;  $loop = mojo::ioloop->singleton;  $now = 1; $timeout = 3; $loop->recurring( 1 => sub {   print $now++ . "\n";   boom() unless $timeout--; });  $loop->timer( 2 => sub {    print "event resets. no boom yet\n";   $timeout = 3; });  $loop->start;  sub boom {    print "boom!\n";   $loop->stop; } 

the above method more efficient if expect going have many resets. here example less efficient more direct example. in case, idea keep id of timer can remove , add another. resets timer.

#!/usr/bin/env perl  use mojo::base -strict; use mojo::ioloop;  $loop = mojo::ioloop->singleton;  $now = 1; $loop->recurring( 1 => sub { print $now++ . "\n" } );  $timer = $loop->timer( 3 => \&boom );  $loop->timer( 2 => sub {    print "event resets. no boom yet\n";   $loop->remove($timer);   $timer = $loop->timer( 3 => \&boom ); });  $loop->start;  sub boom {    print "boom!\n";   $loop->stop; } 

note recurring event used here show elapsed time , isn't important flow.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -