in reply to POE::Session - why does alarm order matter?

Your understanding of $_KERNEL->alarm() is slightly off. It will clear any undispatched alarms with the same event name (for the same session) before setting the new one. Compare it to alarm_add(), which adds another alarm without clearing the previous one.

In your broken case, "wakeup" is always dispatched before "wokeup" because it's a fraction of a second earlier (due to Time::HiRes::time()). Every time "wakeup" is dispatched, it pushes "wokeup" 2sec into the future... so "wokeup" never has a chance to be dispatched.

Actually, it behaves the same way even if "wakeup" and "wokeup" are set for the same time. POE breaks ties by dispatching timers in the order they were set.

  • Comment on Re: POE::Session - why does alarm order matter?

Replies are listed 'Best First'.
Re^2: POE::Session - why does alarm order matter?
by jfroebe (Parson) on Jul 02, 2008 at 02:14 UTC

    Thanks! It's clear now :) I added alarm_remove() to remove the old alarms

    Working code:

    #!/usr/bin/perl use strict; use warnings; use POE; use Time::HiRes qw(time); POE::Session->create ( inline_states => { _start => sub { $_[KERNEL]->alias_set('timer'); $_[KERNEL]->post('timer', 'wakeup'); }, wakeup => sub { print "wakeup at ", scalar localtime(), "\n"; $_[KERNEL]->alarm_remove( $_[HEAP]{wakeup_alarm_id} ) if $ +_[HEAP]{wakeup_alarm_id}; $_[HEAP]{wakeup_alarm_id} = $_[KERNEL]->alarm_add( wakeup +=> int( time() ) + 2 ); $_[HEAP]{wokeup_alarm_id} = $_[KERNEL]->alarm_add( wokeup +=> int( time() ) + 2 ); }, wokeup => sub { $_[KERNEL]->alarm_remove( $_[HEAP]{wokeup_alarm_id} ) if $ +_[HEAP]{wokeup_alarm_id}; print "wokeup at ", scalar localtime(), "\n"; }, }, ); POE::Kernel->run(); exit 0;

    Jason L. Froebe

    Blog, Tech Blog