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

Comparing cvsroot/AnyEvent-MP/MP.pm (file contents):
Revision 1.50 by root, Fri Aug 14 14:01:05 2009 UTC vs.
Revision 1.53 by root, Fri Aug 14 15:31:21 2009 UTC

23 snd $port, type => data...; 23 snd $port, type => data...;
24 snd $port, @msg; 24 snd $port, @msg;
25 snd @msg_with_first_element_being_a_port; 25 snd @msg_with_first_element_being_a_port;
26 26
27 # creating/using ports, the simple way 27 # creating/using ports, the simple way
28 my $somple_port = port { my @msg = @_; 0 }; 28 my $simple_port = port { my @msg = @_; 0 };
29 29
30 # creating/using ports, type matching 30 # creating/using ports, tagged message matching
31 my $port = port; 31 my $port = port;
32 rcv $port, ping => sub { snd $_[0], "pong"; 0 }; 32 rcv $port, ping => sub { snd $_[0], "pong"; 0 };
33 rcv $port, pong => sub { warn "pong received\n"; 0 }; 33 rcv $port, pong => sub { warn "pong received\n"; 0 };
34 34
35 # create a port on another node 35 # create a port on another node
69 69
70=item port 70=item port
71 71
72A port is something you can send messages to (with the C<snd> function). 72A port is something you can send messages to (with the C<snd> function).
73 73
74Some ports allow you to register C<rcv> handlers that can match specific 74Ports allow you to register C<rcv> handlers that can match all or just
75messages. All C<rcv> handlers will receive messages they match, messages 75some messages. Messages will not be queued.
76will not be queued.
77 76
78=item port id - C<noderef#portname> 77=item port id - C<noderef#portname>
79 78
80A port id is normaly the concatenation of a noderef, a hash-mark (C<#>) as 79A port ID is the concatenation of a noderef, a hash-mark (C<#>) as
81separator, and a port name (a printable string of unspecified format). An 80separator, and a port name (a printable string of unspecified format). An
82exception is the the node port, whose ID is identical to its node 81exception is the the node port, whose ID is identical to its node
83reference. 82reference.
84 83
85=item node 84=item node
86 85
87A node is a single process containing at least one port - the node 86A node is a single process containing at least one port - the node port,
88port. You can send messages to node ports to find existing ports or to 87which provides nodes to manage each other remotely, and to create new
89create new ports, among other things. 88ports.
90 89
91Nodes are either private (single-process only), slaves (connected to a 90Nodes are either private (single-process only), slaves (connected to a
92master node only) or public nodes (connectable from unrelated nodes). 91master node only) or public nodes (connectable from unrelated nodes).
93 92
94=item noderef - C<host:port,host:port...>, C<id@noderef>, C<id> 93=item noderef - C<host:port,host:port...>, C<id@noderef>, C<id>
143 kil $SELF, die => $msg; 142 kil $SELF, die => $msg;
144} 143}
145 144
146=item $thisnode = NODE / $NODE 145=item $thisnode = NODE / $NODE
147 146
148The C<NODE> function returns, and the C<$NODE> variable contains 147The C<NODE> function returns, and the C<$NODE> variable contains the
149the noderef of the local node. The value is initialised by a call 148noderef of the local node. The value is initialised by a call to
150to C<become_public> or C<become_slave>, after which all local port 149C<initialise_node>.
151identifiers become invalid.
152 150
153=item $noderef = node_of $port 151=item $noderef = node_of $port
154 152
155Extracts and returns the noderef from a portid or a noderef. 153Extracts and returns the noderef from a port ID or a noderef.
156 154
157=item initialise_node $noderef, $seednode, $seednode... 155=item initialise_node $noderef, $seednode, $seednode...
158 156
159=item initialise_node "slave/", $master, $master... 157=item initialise_node "slave/", $master, $master...
160 158
279=item snd $port, type => @data 277=item snd $port, type => @data
280 278
281=item snd $port, @msg 279=item snd $port, @msg
282 280
283Send the given message to the given port ID, which can identify either 281Send the given message to the given port ID, which can identify either
284a local or a remote port, and can be either a string or soemthignt hat 282a local or a remote port, and must be a port ID.
285stringifies a sa port ID (such as a port object :).
286 283
287While the message can be about anything, it is highly recommended to use a 284While the message can be about anything, it is highly recommended to use a
288string as first element (a portid, or some word that indicates a request 285string as first element (a port ID, or some word that indicates a request
289type etc.). 286type etc.).
290 287
291The message data effectively becomes read-only after a call to this 288The message data effectively becomes read-only after a call to this
292function: modifying any argument is not allowed and can cause many 289function: modifying any argument is not allowed and can cause many
293problems. 290problems.
738convenience functionality. 735convenience functionality.
739 736
740This means that AEMP requires a less tightly controlled environment at the 737This means that AEMP requires a less tightly controlled environment at the
741cost of longer node references and a slightly higher management overhead. 738cost of longer node references and a slightly higher management overhead.
742 739
740=item Erlang has a "remote ports are like local ports" philosophy, AEMP
741uses "local ports are like remote ports".
742
743The failure modes for local ports are quite different (runtime errors
744only) then for remote ports - when a local port dies, you I<know> it dies,
745when a connection to another node dies, you know nothing about the other
746port.
747
748Erlang pretends remote ports are as reliable as local ports, even when
749they are not.
750
751AEMP encourages a "treat remote ports differently" philosophy, with local
752ports being the special case/exception, where transport errors cannot
753occur.
754
743=item * Erlang uses processes and a mailbox, AEMP does not queue. 755=item * Erlang uses processes and a mailbox, AEMP does not queue.
744 756
745Erlang uses processes that selctively receive messages, and therefore 757Erlang uses processes that selectively receive messages, and therefore
746needs a queue. AEMP is event based, queuing messages would serve no useful 758needs a queue. AEMP is event based, queuing messages would serve no
747purpose. 759useful purpose. For the same reason the pattern-matching abilities of
760AnyEvent::MP are more limited, as there is little need to be able to
761filter messages without dequeing them.
748 762
749(But see L<Coro::MP> for a more Erlang-like process model on top of AEMP). 763(But see L<Coro::MP> for a more Erlang-like process model on top of AEMP).
750 764
751=item * Erlang sends are synchronous, AEMP sends are asynchronous. 765=item * Erlang sends are synchronous, AEMP sends are asynchronous.
752 766
753Sending messages in Erlang is synchronous and blocks the process. AEMP 767Sending messages in Erlang is synchronous and blocks the process (and
754sends are immediate, connection establishment is handled in the 768so does not need a queue that can overflow). AEMP sends are immediate,
755background. 769connection establishment is handled in the background.
756 770
757=item * Erlang can silently lose messages, AEMP cannot. 771=item * Erlang suffers from silent message loss, AEMP does not.
758 772
759Erlang makes few guarantees on messages delivery - messages can get lost 773Erlang makes few guarantees on messages delivery - messages can get lost
760without any of the processes realising it (i.e. you send messages a, b, 774without any of the processes realising it (i.e. you send messages a, b,
761and c, and the other side only receives messages a and c). 775and c, and the other side only receives messages a and c).
762 776
774eventually be killed - it cannot happen that a node detects a port as dead 788eventually be killed - it cannot happen that a node detects a port as dead
775and then later sends messages to it, finding it is still alive. 789and then later sends messages to it, finding it is still alive.
776 790
777=item * Erlang can send messages to the wrong port, AEMP does not. 791=item * Erlang can send messages to the wrong port, AEMP does not.
778 792
779In Erlang it is quite possible that a node that restarts reuses a process 793In Erlang it is quite likely that a node that restarts reuses a process ID
780ID known to other nodes for a completely different process, causing 794known to other nodes for a completely different process, causing messages
781messages destined for that process to end up in an unrelated process. 795destined for that process to end up in an unrelated process.
782 796
783AEMP never reuses port IDs, so old messages or old port IDs floating 797AEMP never reuses port IDs, so old messages or old port IDs floating
784around in the network will not be sent to an unrelated port. 798around in the network will not be sent to an unrelated port.
785 799
786=item * Erlang uses unprotected connections, AEMP uses secure 800=item * Erlang uses unprotected connections, AEMP uses secure

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines