how to find remaining time on a timer?

Maxime Soulé btik-cpan at scoubidou.com
Thu Nov 28 17:18:17 CET 2013


Hi,

Le 28.11.2013 16:30, Fulko Hew a écrit :
> I have a scenario where I need to find out how much time
> is remaining on a timer (so that I can perform some calculation
> and then re-schedule something, based on that calculation).
>
> How can I find out something about the timer?
> Either when it was started, how much time has elapsed,
> or how much time is remaining?

The AnyEvent->timer returned object is created by the underlying event loop.

You have to look at its documentation to find how to introspect these 
timer objects and so find whether it is possible or not... But keep in 
mind that in this case, your AnyEvent code won't accept another 
underlying event loop anymore.

Else, you can use a closure for your short timer, that use a variable 
containing the start date of your long timer. It is not very accurate 
but in most cases it will suffice.

my $starttime = time;
my $long = AnyEvent->timer(
                 after   => 5,
                 cb      => sub { print "long timer done\n"; exit; }
                 );
my $short = AnyEvent->timer(
                 after   => 2,
                 cb      => sub {
                             print "time info part way through is: ",
                                     time - $starttime, "\n";
                             }
                 );
...

Regards,

Max.

>
> I tried the following test (based on what I read about EV)
> without success.
>
> use AnyEvent;
>
>     # make a long timer, and a short timer that can 'look into'
>     # the long timer to see what it can see
>
> my $long = AnyEvent->timer(
>                 after   => 5,
>                 cb      => sub { print "long timer done\n"; exit; }
>                 );
> my $short = AnyEvent->timer(
>                 after   => 2,
>                 cb      => sub {
>                             print "time info part way through is: ",
>                                     $long->at, "\n";
>                             }
>                 );
>
> my $cv = AnyEvent->condvar;
> $cv->wait;
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.schmorp.de/pipermail/anyevent/attachments/20131128/b7818a99/attachment.html>


More information about the anyevent mailing list