Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

POE sharing data between sessions

by elwarren (Curate)
on May 17, 2005 at 19:00 UTC ( #457950=perlquestion: print w/ replies, xml ) Need Help??
elwarren has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I've been working on a toy with POE recently. A simple bot that sits on our private IRC network and collects some stats using POE::Component::IRC. I'd like to share those stats out via POE::Component::Server::SimpleHTTP.

I'm wondering what is the best way to share data between sessions in a POE environment? Sessions are seperate, so they do not share HEAP. I could just create a global hash, but that just doesn't seem to fit into the POE model. Maybe it does and I'm trying to make it too difficult :-)

I've tried (without much success) to access a remote HEAP:
my $ircsession = $kernel->alias_resolve('irc_bot'); my $heap = $ircsession->get_heap(); foreach (keys %{$heap->{USERLIST}} ) {
Perhaps I should have a wheel that takes my irc events and pushes them into a database, but that's overkill. Pushing from the irc session isn't the issue, it's pulling the current state from the http side that is twisting my brain.

Ah well, such is programming with POE...

Comment on POE sharing data between sessions
Download Code
Re: POE sharing data between sessions
by johnnywang (Priest) on May 17, 2005 at 20:11 UTC
    You can use the session id of the first session to look it up, and then call its get_heap() method. I'm not sure this is "the" preferred way, the following code works fine to access the heap of first session from within an event handler of the second:
    use strict; use POE; use POE::Wheel::SocketFactory; my $session1 = POE::Session->create( inline_states => { _start => \&start1, } ); my $session1_id = $session1->ID(); POE::Session->create( inline_states => { _start => \&start2, access_session1 => \&access, }, ); POE::Kernel->run(); sub start1 { # start something so the session doesn't go away. $_[HEAP]->{factory} = POE::Wheel::SocketFactory->new( BindPort => '88888', SocketProtocol => 'tcp', Reuse => 'on', SuccessEvent => 'event_factory_success', FailureEvent => 'event_fatal_error', ); $_[HEAP]->{"test"} = {'a'=>1,'b'=>2}; } sub start2 { #fire an event $_[KERNEL]->yield("access_session1"); } sub access { my $sess = $_[KERNEL]->alias_resolve($session1_id); my $data = $sess->get_heap()->{"test"}; foreach my $k (keys %$data){ print "$k, $data->{$k}\n"; } } __OUTPUT__ a, 1 b, 2
      Ah ha. Instead of passing my sessiond id, I was passing my session alias to the alias_resolve call. Thanks, that solves one of my problems. I'm going to pass the $session1->ID onto the HEAP during create of $session2.

      Still wouldn't mind hearing about heap vs global performance/gotchas if anyone cares to shared.

      I've looked around and can't seem to find a way to list my current sessions dynamically. Ideally I was hoping for a solution more like this (completely not real code):
      foreach my $sess ( @{ $kernel->get_sessions_named('irc_bot') } ) { print $sess->get_heap()->{USERLIST}; }
Re: POE sharing data between sessions
by rcaputo (Chaplain) on Jun 10, 2005 at 21:10 UTC

    It's okay to use global variables can act as shared memory between POE sessions. If you'd rather not use a global hash, you can always create a singleton object to act as an in-memory database for shared information. POE's web site has a chat server example that uses a global hash and an accessor to keep track of the users currently online.

    POE's not nearly as fascist as people seem to think it is.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://457950]
Approved by jfroebe
Front-paged by jfroebe
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2014-09-21 01:39 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (165 votes), past polls