POE::Kernel - POE Event Queue and Resource Manager


NAME

POE::Kernel - POE Event Queue and Resource Manager


SUPPORTED PLATFORMS

This module is not included with the standard ActivePerl distribution. It is available as a separate download using PPM.

SYNOPSIS

  #!/usr/bin/perl -w
  use strict;
  use POE;                 # Includes POE::Kernel and POE::Session
  print $poe_kernel->ID(); # This process' unique ID.
  new POE::Session( ... ); # Bootstrap sessions are here.
  $poe_kernel->run();      # Run the kernel.
  exit;                    # Exit when the kernel's done.
  # Session management methods:
  $kernel->session_create( ... );
  # Event management methods:
  $kernel->post( $session, $state, @args );
  $kernel->yield( $state, @args );
  $kernel->call( $session, $state, @args );
  # Alarms and timers:
  $kernel->alarm( $state, $time, @args );
  $kernel->delay( $state, $seconds, @args );
  # Aliases:
  $status = $kernel->alias_set( $alias );
  $status = $kernel->alias_remove( $alias );
  $session_reference = $kernel->alias_resolve( $alias );
  # Selects:
  $kernel->select( $file_handle,
                   $read_state_name,     # or undef to remove it
                   $write_state_name,    # or undef to remove it
                   $expedite_state_same, # or undef to remove it
                 );
  $kernel->select_read( $file_handle, $read_state_name );
  $kernel->select_write( $file_handle, $write_state_name );
  $kernel->select_expedite( $file_handle, $expedite_state_name );
  # Signals:
  $kernel->sig( $signal_name, $state_name ); # Registers a handler.
  $kernel->signal( $session, $signal_name ); # Posts a signal.
  # Processes.
  $kernel->fork();   # "Safe" fork that polls for SIGCHLD.
  # States:
  $kernel->state( $state_name, $code_reference );    # Inline state
  $kernel->state( $method_name, $object_reference ); # Object state
  $kernel->state( $function_name, $package_name );   # Package state
  $kernel->state( $state_name,                       # Object or package
                  $object_or_package_reference,      #  state, mapped to
                  $object_or_package_method,         #  different method.
                );
  # IDs:
  $kernel->ID();                       # Return the Kernel's unique ID.
  $kernel->ID_id_to_session($id);      # Return undef, or the ID's session.
  $kernel->ID_session_to_id($session); # Return undef, or the session's ID.


DESCRIPTION

POE::Kernel contains POE's event loop, select logic and resource management methods. There can only be one POE::Kernel per process, and it's created automatically the first time POE::Kernel is used. This simplifies signal delivery in the present and threads support in the future.


EXPORTED SYMBOLS

POE::Kernel exports $poe_kernel, a reference to the program's single kernel instance. This mainly is used in the main package, so that $poe_kernel->run() may be called cleanly.

Sessions' states should endeavor to use $_[KERNEL], since $poe_kernel may not be available, or it may be different than the kernel actually invoking the object.


PUBLIC KERNEL METHODS

POE::Kernel contains methods to manage the kernel itself, sessions, and resources such as files, signals and alarms.

Many of the public Kernel methods generate events. Please see the ``PREDEFINED EVENTS AND PARAMETERS'' section in POE::Session's documentation.

Kernel Management Methods

Session Management Methods

Event Management Methods

Events tell sessions which state to invoke next. States are defined when sessions are created. States may also be added, removed or changed at runtime by POE::Kernel::state(), which acts on the current session.

There are a few ways to send events to sessions. Events can be posted, in which case the kernel queues them and dispatches them in FIFO order. States can also be called immediately, bypassing the queue. Immediate calls can be useful for ``critical sections''; for example, POE's I/O abstractions use call() to minimize event latency.

To learn more about events and the information they convey, please see ``PREDEFINED EVENTS AND PARAMETERS'' in the POE::Session documentation.

Alarm Management Methods

Alarms are just events that are scheduled to be dispatched at some later time. POE's queue is a priority queue keyed on time, so these events go to the appropriate place in the queue. Posted events are really enqueued for ``now'' (defined as whatever time() returns).

If Time::HiRes is available, POE will use it to achieve better resolution on enqueued events.

Alias Management Methods

Aliases allow sessions to be referenced by name instead of by session reference. They also allow sessions to remain active without having selects or events. This provides support for ``daemon'' sessions that act as resources but don't necessarily have resources themselves.

Aliases must be unique. Sessions may have more than one alias.

Select Management Methods

Selects are file handle monitors. They generate events to indicate when activity occurs on the file handles they watch. POE keeps track of how many selects are watching a file handle, and it will close the file when nobody is looking at it.

There are three types of select. Each corresponds to one of the bit vectors in Perl's four-argument select() function. ``Read'' selects generate events when files become ready for reading. ``Write'' selects generate events when files are available to be written to. ``Expedite'' selects generate events when files have out-of-band information to be read.

Signal Management Methods

The POE::Session documentation has more information about _signal events.

POE currently does not make Perl's signals safe. Using signals is okay in short-lived programs, but long-uptime servers may eventually dump core if they receive a lot of signals. POE provides a ``safe'' fork() function that periodically reaps children without using signals; it emulates the system's SIGCHLD signal for each process in reaps.

Mileage varies considerably.

The kernel generates _signal events when it receives signals from the operating system. Sessions may also send signals between themselves without involving the OS.

The kernel determines whether or not signals have been handled by looking at _signal states' return values. If the state returns logical true, then it means the signal was handled. If it returns false, then the kernel assumes the signal wasn't handled.

POE will stop sessions that don't handle some signals. These ``terminal'' signals are QUIT, INT, KILL, TERM, HUP, and the fictitious IDLE signal.

POE broadcasts SIGIDLE to all sessions when the kernel runs out of events to dispatch, and when there are no alarms or selects to generate new events.

Finally, there is one fictitious signal that always stops a session: ZOMBIE. If the kernel remains idle after SIGIDLE is broadcast, then SIGZOMBIE is broadcast to force reaping of zombie sessions. This tells these sessions (usually aliased ``daemon'' sessions) that nothing is left to do, and they're as good as dead anyway.

It's normal for aliased sessions to receive IDLE and ZOMBIE when all the sessions that may use them have gone away.

Process Management Methods

POE's signal handling is Perl's signal handling, which means that POE won't safely handle signals as long as Perl has a problem with them.

However, POE works around this in at least SIGCHLD's case by providing a ``safe'' fork() function. &POE::Kernel::fork() blocks $SIG{'CHLD','CLD'} and starts an event loop to poll for expired child processes. It emulates the system's SIGCHLD behavior by sending a ``soft'' CHLD signal to the appropriate session.

Because POE knows which session called its version of fork(), it can signal just that session that its forked child process has completed.

Note: The first &POE::Kernel::fork call disables POE's usual SIGCHLD handler, so that the poll loop can reap children safely. Mixing plain fork and &POE::Kernel::fork isn't recommended.

State Management Methods

The kernel's state management method lets sessions add, change and remove states at runtime. Wheels use this to add and remove select states from sessions when they're created and destroyed.

ID Management Methods

POE generates a unique ID for the process, and it maintains unique serial numbers for every session. These functions retrieve various ID values.


SEE ALSO

POE; POE::Session


BUGS

Oh, probably some.


AUTHORS & COPYRIGHTS

Please see the POE manpage.

 POE::Kernel - POE Event Queue and Resource Manager