ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/Intro.pod
(Generate patch)

Comparing AnyEvent-MP/MP/Intro.pod (file contents):
Revision 1.23 by root, Sat Aug 29 14:36:10 2009 UTC vs.
Revision 1.24 by root, Sat Aug 29 15:13:36 2009 UTC

68a few simple examples. Later some more complex idioms are introduced, 68a few simple examples. Later some more complex idioms are introduced,
69which are hopefully useful to solve some real world problems. 69which are hopefully useful to solve some real world problems.
70 70
71=head1 Passing Your First Message 71=head1 Passing Your First Message
72 72
73As start lets have a look at the messaging API. The next example is just a 73As a start lets have a look at the messaging API. The following example
74demo to show the basic elements of message passing with L<AnyEvent::MP>. 74is just a demo to show the basic elements of message passing with
75It shout just print: "Ending with: 123". So here the code: 75L<AnyEvent::MP>.
76
77The example should print: C<Ending with: 123>, in a rather complicated
78way, by passing some message to a port.
76 79
77 use AnyEvent; 80 use AnyEvent;
78 use AnyEvent::MP; 81 use AnyEvent::MP;
79 82
80 my $end_cv = AnyEvent->condvar; 83 my $end_cv = AnyEvent->condvar;
88 91
89 snd $port, test => 123; 92 snd $port, test => 123;
90 93
91 print "Ending with: " . $end_cv->recv . "\n"; 94 print "Ending with: " . $end_cv->recv . "\n";
92 95
93It already contains most functions of the essential L<AnyEvent::MP> API. 96It already uses most of the essential functions inside
97L<AnyEvent::MP>: First there is the C<port> function which will create a
98I<port> and will return it's I<port ID>, a simple string.
94 99
95First there is the C<port> function which will create a I<port> and will return 100This I<port ID> can be used to send messages to the port and install
96it's I<port id>. 101handlers to receive messages on the port. Since it is a simple string
97
98That I<port id> can be used to send and receive messages. That I<port id> is a
99simple string and can be safely passed to other I<nodes> in the network to 102it can be safely passed to other I<nodes> in the network when you want
100refer to that specific port (usually used for RPC, where you need to 103to refer to that specific port (usually used for RPC, where you need
101tell the other end which I<port> to send the reply to). 104to tell the other end which I<port> to send the reply to - messages in
105L<AnyEvent::MP> have a destination, but no source).
102 106
103Next function is C<rcv>: 107The next function is C<rcv>:
104 108
105 rcv $port, test => sub { ... }; 109 rcv $port, test => sub { ... };
106 110
107It sets up a receiver callback on a specific I<port> which needs to be 111It installs a receiver callback on the I<port> that specified as the first
108specified as the first argument. The next argument, in this example C<test>, is 112argument (it only works for "local" ports, i.e. ports created on the same
113node). The next argument, in this example C<test>, specifies a I<tag> to
109a I<tag> match. This means that whenever a message, with the first element 114match. This means that whenever a message with the first element being
110being the string C<tag>, is received the callback is called with the remaining 115the string C<test> is received, the callback is called with the remaining
111parts of that message. 116parts of that message.
112 117
113Messages can be send with the C<snd> function, which looks like this in the 118Messages can be sent with the C<snd> function, which is used like this in
114example above: 119the example above:
115 120
116 snd $port, test => 123; 121 snd $port, test => 123;
117 122
118This will send the message C<['test', 123]> to the I<port> with the I<port id> 123This will send the message C<'test', 123> to the I<port> with the I<port
119in C<$port>. The receiver got a I<tag> match on C<test> and will call the 124ID> stored in C<$port>. Since in this case the receiver has a I<tag> match
120callback with the first argument being the number C<123>. 125on C<test> it will call the callback with the first argument being the
126number C<123>.
121 127
122That callback then just passes that number on to the I<condition variable> 128The callback is a typicall AnyEvent idiom: the callback just passes
123C<$end_cv> which will then pass the value to the print. But I<condition 129that number on to the I<condition variable> C<$end_cv> which will then
124variables> are out of the scope of this tutorial. So please consult the 130pass the value to the print. Condition variables are out of the scope
131of this tutorial and not often used with ports, so please consult the
125L<AnyEvent::Intro> about them. 132L<AnyEvent::Intro> about them.
126 133
127But passing messages inside one process is boring, but before we can continue 134Passing messages inside just one process is boring. Before we can move on
128and take the next step to interprocess message passing we first have to make 135and do interprocess message passing we first have to make sure some things
129sure some things have been setup. 136have been set up correctly for our nodes to talk to each other.
130 137
131=head1 System Requirements and System Setup 138=head1 System Requirements and System Setup
132 139
133Before we can start with real IPC we have to make sure some things work on your 140Before we can start with real IPC we have to make sure some things work on your
134system. 141system.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines