Home > Archive > PERL POE > April 2007 > POE newbie question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
POE newbie question
|
|
| Fei Liu 2007-02-22, 7:24 pm |
| Hi group, just wondering what's the difference between
POE::Kernel::select_read/write method and POE::Kernel::yield method? How
should I use select_read/select_write? Do they do anything special about
the events that they select? Thanks,
Fei
| |
| Matt Sickler 2007-02-22, 7:24 pm |
| select_read and select_write tell POE::Kernel to watch a filehandle for
those states
$poe_kernel->yield() its NOT like Thread's yield(). NOT AT ALL.
read the perldoc on POE::Kernel to see what yield() does (ditto on the other
two)
POE is quite well documented
On 2/22/07, Fei Liu <feiliu@aepnetworks.com> wrote:
>
> Hi group, just wondering what's the difference between
> POE::Kernel::select_read/write method and POE::Kernel::yield method? How
> should I use select_read/select_write? Do they do anything special about
> the events that they select? Thanks,
>
> Fei
>
| |
| Fei Liu 2007-02-23, 7:24 pm |
| P Dobranski wrote:[color=darkred]
> POE::Kernel::yield is sort of a shortcut for 'post'.
> You 'yield' an event in the current session.
> It is the same as 'post'ing the event to the current session.
>
> $kernel->yield('some_event_handler');
>
> is roughly the same as
> $kernel->post($my_session, 'some_event_handler');
>
> (assuming both statements happen in the same session).
>
> select_read/select_write allow you to get events when a file
> descriptor becomes ready to read or write without you having to manage
> your own select() loops.
>
> On 2/22/07, Fei Liu <feiliu@aepnetworks.com> wrote:
Thanks for your reply. I was reading Rocco's tutorial
(http://poe.perl.org/?Evolution_of_a_POE_Server) on POE and my question
came up when he first introduced POE framework to replace the original
non-blocking tcp echo server. In the example no Wheel or
Component::Server::TCP were used, it was basic POE::Session.
POE::Session->create
( inline_states =>
{ _start => \&server_start,
event_accept => \&server_accept,
event_read => \&client_read,
event_write => \&client_write,
event_error => \&client_error,
}
);
sub server_start {
my $server = IO::Socket::INET->new
( LocalPort => 12345,
Listen => 10,
Reuse => "yes",
) or die "can't make server socket: $@\n";
$_[KERNEL]->select_read( $server, "event_accept" );
}
Here it seems select_read works sort of like yield, doesn't it? It
yields to event_accept handler? Quote 'The _start handler creates the
server socket and allocates its event generator with select_read()",
what's a event generator and what does it do?
Another thing confusing me here is that there seems to be a total of one
session in this echo server implementation, how can it handle mutliple
clients?
Sorry for so many questions, I can use POE just fine but I want to
understand what's going on under the hood.
Fei
| |
| Matt Sickler 2007-02-23, 7:24 pm |
| select_read( $socket, "event to fire") tells the kernel to fire the event
"event to fire" to the current session whenever there is data that can be
read from $socket
yield("event") tells the kernel to queue the signal, event, whatever you
want to call it, to the current session
On 2/23/07, Fei Liu <feiliu@aepnetworks.com> wrote:
>
> P Dobranski wrote:
> How
> about
>
> Thanks for your reply. I was reading Rocco's tutorial
> (http://poe.perl.org/?Evolution_of_a_POE_Server) on POE and my question
> came up when he first introduced POE framework to replace the original
> non-blocking tcp echo server. In the example no Wheel or
> Component::Server::TCP were used, it was basic POE::Session.
>
> POE::Session->create
> ( inline_states =>
> { _start => \&server_start,
> event_accept => \&server_accept,
> event_read => \&client_read,
> event_write => \&client_write,
> event_error => \&client_error,
> }
> );
>
> sub server_start {
> my $server = IO::Socket::INET->new
> ( LocalPort => 12345,
> Listen => 10,
> Reuse => "yes",
> ) or die "can't make server socket: $@\n";
>
> $_[KERNEL]->select_read( $server, "event_accept" );
> }
>
> Here it seems select_read works sort of like yield, doesn't it? It
> yields to event_accept handler? Quote 'The _start handler creates the
> server socket and allocates its event generator with select_read()",
> what's a event generator and what does it do?
>
> Another thing confusing me here is that there seems to be a total of one
> session in this echo server implementation, how can it handle mutliple
> clients?
>
> Sorry for so many questions, I can use POE just fine but I want to
> understand what's going on under the hood.
>
> Fei
>
>
| |
| Rocco Caputo 2007-02-23, 7:24 pm |
| On Feb 23, 2007, at 09:34, Fei Liu wrote:
>
> Here it seems select_read works sort of like yield, doesn't it? It
> yields to event_accept handler? Quote 'The _start handler creates
> the server socket and allocates its event generator with select_read
> ()", what's a event generator and what does it do?
It is somewhat like yield(), except that it only sends the event when
a filehandle has data ready to be read. yield() however sends the
event at the next available opportunity.
Event generators are things that generate events in response to
external stimuli. select_read() manages an event generator within
POE::Kernel. Also consider delay(), which generates an event after a
certain time has elapsed.
Your code can also be one or more event generators. Any time you
call post() or yield() you're generating a new event.
> Another thing confusing me here is that there seems to be a total
> of one session in this echo server implementation, how can it
> handle mutliple clients?
A single session can handle multiple clients. The handler for
"event_accept" accepts each new connection and calls select_read() to
be made aware when the client sends data. While there are active
connections, the one session is watching multiple filehandles at once.
The client_read() function described later in the article handles
data sent by each client. The $client parameter is a copy of the
client's socket. It separates each client's data by using hashes
keyed on the client sockets. For example, $inbuffer{$client} and
$outbuffer{$client} are the client connection's input and output
buffers.
There will be no hash collisions since no two clients will have the
same socket at the same time.
Conscientious deletion of old data from the hashes is important
here. A new connection may have the same socket as a previously
closed one, and you don't want data to bleed between connections.
Likewise, you may see memory or other leaks if you don't clean up the
hashes as clients disconnect.
> Sorry for so many questions, I can use POE just fine but I want to
> understand what's going on under the hood.
I'm all for that. :)
--
Rocco Caputo - rcaputo@pobox.com
| |
|
|
|
|
|
|
|
|
|