KaiLoi has asked for the wisdom of the Perl Monks concerning the following question:

Hi there,

I'm having a bit of trouble with Alarm timers in Perl POE, I have a program that does a couple of things periodically

However my alarm timers appear to be stomping on one another. The first loop around the wheel they both run they work fine the alert alarm happens at 2, then 4 seconds. Then the Mount alarm triggers at 5 seconds, then they both just start triggering every 2 seconds. I can see I must be stomping on the mounts alarm with the alerts alarm assignment, and I've tried a lot of things like naming the alias different things, naming "next_alarm_time" different things between the subs, naming the subs "tick" and "tock" but nothing seems to work. Any suggestions?

#!/usr/bin/perl use warnings; use strict; use POE qw(Wheel::Run Filter::Reference Wheel::FollowTail); POE::Session->create( inline_states => { _start => sub { $_[KERNEL]->alias_set('MountWatchdog'); $_[HEAP]->{next_alarm_time} = int(time()) + 5; $_[KERNEL]->alarm(tick => $_[HEAP]->{next_alarm_time}); }, tick => sub { my $key; my $name; print "Mount tick at ", time(), "\n"; $_[HEAP]->{next_alarm_time} = $_[HEAP]->{next_alarm_time} + 2; $_[KERNEL]->alarm(tick => $_[HEAP]->{next_alarm_time}); }, }, ); POE::Session->create( inline_states => { _start => sub { $_[KERNEL]->alias_set('AlertWatchdog'); $_[HEAP]->{next_alarm_time} = int(time()) + 2; $_[KERNEL]->alarm(tick => $_[HEAP]->{next_alarm_time}); }, tick => sub { my $key; my $name; print "Alert tick at ", time(), "\n"; $_[HEAP]->{next_alarm_time} = $_[HEAP]->{next_alarm_time} + 2; $_[KERNEL]->alarm(tick => $_[HEAP]->{next_alarm_time}); }, }, ); # Start our POE Kernel. $poe_kernel->run(); exit 0;

Replies are listed 'Best First'.
Re: Trying to implement 2 seperate alarm timers in POE
by Anonymous Monk on Oct 13, 2017 at 02:16 UTC
    In MountWatchdog's "tick" handler you are adding 2 to next_alarm_time. It sounds like you want to be adding 5.