The short answer: POE doesn't have a public method to query the event queue. If it did, the generic "grep the event queue" would be slower than just checking a flag.

The medium answer: POE::Queue::Array has a generic query mechanism, but POE::Kernel doesn't expose it. Even if it did, searching the queue would be an O(N) operation while maintaining and checking a flag are O(1).

The long answer:

POE's event queue is an ordered heap, implemented using a Perl array. It's done this way to make inserting events fast (that's O(N) for small queues and O(log N) for large ones). Removing the next event is always an O(1) operation.

So POE is optimized for what you do most of the time: Posting events, setting timers, and dequeuing the next event. Actually finding an event in the queue is not optimize, so it's quicker (although more work for you) to manage a flag.

You could create a small function library to keep track of special one-shot timers, though. Something like this untested code:

my %pending; sub set_oneshot_timer { my ($event, $hence) = @_; my $session_id = $poe_kernel->get_active_session()->ID(); return if exists $pending{$session_id}{$event}; $poe_kernel->delay( $event, $hence ); $pending{$session_id}{$event} = 1; } sub caught_oneshot_timer { my $event = shift; my $session_id = $poe_kernel->get_active_session()->ID(); delete $pending{$session_id}{$event}; delete $pending{$session_id} unless keys %{$pending{$session_id}}; }

To use it:

sub some_event_handler { # set a timer for 10s from now, unless it's already set set_oneshot_timer( event_to_send => 10 ); } sub handle_timer_event { # caught the timer; clear the flag caught_oneshot_timer($_[STATE]); }

I hope this helps.

-- Rocco Caputo - http://poe.perl.org/


In reply to Re: Accessing POE's delayed event queue by rcaputo
in thread Accessing POE's delayed event queue by diotalevi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.