Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

POE::Session - why does alarm order matter?

by jfroebe (Vicar)
on Jul 02, 2008 at 01:51 UTC ( #695063=perlquestion: print w/ replies, xml ) Need Help??
jfroebe has asked for the wisdom of the Perl Monks concerning the following question:

When I call $_[KERNEL]->alarm() twice referencing two events from within an event that itself is called by $_[KERNEL]->alarm().

_start -> (alarm to wakeup event). wakeup event -> ( alarms to wokeup event and wakeup event). This works as I would expect.

When I call the alarm to wakeup before the alarm to wokeup, the wokeup event is never called.

works:

#!/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( wokeup => int( time() ) + 2 ); $[KERNEL]->alarm( wakeup => int( time() ) + 2 ); }, wokeup => sub { print "wokeup at ", scalar localtime(), "\n"; }, }, ); POE::Kernel->run(); exit 0;
jason@jfroebe-laptop:~/bin$ ./test_poe.pl wakeup at Tue Jul 1 20:30:56 2008 wokeup at Tue Jul 1 20:30:58 2008 wakeup at Tue Jul 1 20:30:58 2008 wokeup at Tue Jul 1 20:31:00 2008

wokeup is never called:

#!/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( wakeup => int( time() ) + 2 ); $_[KERNEL]->alarm( wokeup => int( time() ) + 2 ); }, wokeup => sub { print "wokeup at ", scalar localtime(), "\n"; }, }, ); POE::Kernel->run(); exit 0;
jason@jfroebe-laptop:~/bin$ ./test_poe.pl wakeup at Tue Jul 1 19:36:29 2008 wakeup at Tue Jul 1 19:36:31 2008 wakeup at Tue Jul 1 19:36:33 2008

My understanding is that $_[KERNEL]->alarm() sets the alarm and moves on. Apparently it isn't when the alarm is set to the event that it is currently in. That, or I'm misunderstanding $_[KERNEL]->alarm(). Am I close?

Jason L. Froebe

Blog, Tech Blog

Comment on POE::Session - why does alarm order matter?
Select or Download Code
Re: POE::Session - why does alarm order matter?
by rcaputo (Chaplain) on Jul 02, 2008 at 02:04 UTC

    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.

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://695063]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2014-08-23 17:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (176 votes), past polls