AnyEvent-MP
view release on metacpan or search on metacpan
MP/Intro.pod view on Meta::CPAN
=head1 Message Passing for the Non-Blocked Mind
=head1 Introduction and Terminology
This is a tutorial about how to get the swing of the L<AnyEvent::MP>
module family, which allows processes to transparently pass messages to
itself and to other processes on the same or a different host.
What kind of messages? Basically a message here means a list of Perl
strings, numbers, hashes and arrays, anything that can be expressed as a
L<JSON> text (as JSON is the default serialiser in the protocol). Here are
two examples:
write_log => 1251555874, "action was successful.\n"
123, ["a", "b", "c"], { foo => "bar" }
When using L<AnyEvent::MP> it is customary to use a descriptive string as
first element of a message that indicates the type of the message. This
element is called a I<tag> in L<AnyEvent::MP>, as some API functions
(C<rcv>) support matching it directly.
Supposedly you want to send some kind of ping message with your current
time to somewhere, this is how such a message might look like (in Perl
syntax):
ping => 1251381636
Now that we know what a message is, to which entities are those
messages being I<passed>? They are I<passed> to I<ports>. A I<port> is
a destination for messages but also a context to execute code: when
a runtime error occurs while executing code belonging to a port, the
exception will be raised on the port and can even travel to interested
parties on other nodes, which makes supervision of distributed processes
easy.
How do these ports relate to things you know? Each I<port> belongs
to a I<node>, and a I<node> is just the UNIX process that runs your
L<AnyEvent::MP> application.
Each I<node> is distinguished from other I<nodes> running on the same or
another host in a network by its I<node ID>. A I<node ID> is simply a
unique string chosen manually or assigned by L<AnyEvent::MP> in some way
(UNIX nodename, random string...).
Here is a diagram about how I<nodes>, I<ports> and UNIX processes relate
to each other. The setup consists of two nodes (more are of course
possible): Node C<A> (in UNIX process 7066) with the ports C<ABC> and
C<DEF>. And the node C<B> (in UNIX process 8321) with the ports C<FOO> and
C<BAR>.
|- PID: 7066 -| |- PID: 8321 -|
| | | |
| Node ID: A | | Node ID: B |
| | | |
| Port ABC =|= <----\ /-----> =|= Port FOO |
| | X | |
| Port DEF =|= <----/ \-----> =|= Port BAR |
| | | |
|-------------| |-------------|
The strings for the I<port IDs> here are just for illustrative
purposes: Even though I<ports> in L<AnyEvent::MP> are also identified by
strings, they can't be chosen manually and are assigned by the system
dynamically. These I<port IDs> are unique within a network and can also be
used to identify senders, or even as message tags for instance.
( run in 0.853 second using v1.01-cache-2.11-cpan-39bf76dae61 )