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

Comparing AnyEvent-MP/MP.pm (file contents):
Revision 1.34 by root, Wed Aug 5 23:50:46 2009 UTC vs.
Revision 1.35 by root, Thu Aug 6 10:21:48 2009 UTC

22 snd $port2, ping => $port1; 22 snd $port2, ping => $port1;
23 23
24 # more, smarter, matches (_any_ is exported by this module) 24 # more, smarter, matches (_any_ is exported by this module)
25 rcv $port, [child_died => $pid] => sub { ... 25 rcv $port, [child_died => $pid] => sub { ...
26 rcv $port, [_any_, _any_, 3] => sub { .. $_[2] is 3 26 rcv $port, [_any_, _any_, 3] => sub { .. $_[2] is 3
27
28 # linking two ports, so they both crash together
29 lnk $port1, $port2;
30
31 # monitoring
32 mon $port, $cb->(@msg) # callback is invoked on death
33 mon $port, $otherport # kill otherport on abnormal death
34 mon $port, $otherport, @msg # send message on death
27 35
28=head1 DESCRIPTION 36=head1 DESCRIPTION
29 37
30This module (-family) implements a simple message passing framework. 38This module (-family) implements a simple message passing framework.
31 39
618 626
619=back 627=back
620 628
621=head1 AnyEvent::MP vs. Distributed Erlang 629=head1 AnyEvent::MP vs. Distributed Erlang
622 630
623AnyEvent::MP got lots of its ideas from distributed erlang (erlang node 631AnyEvent::MP got lots of its ideas from distributed Erlang (Erlang node
624== aemp node, erlang process == aemp port), so many of the documents and 632== aemp node, Erlang process == aemp port), so many of the documents and
625programming techniques employed by erlang apply to AnyEvent::MP. Here is a 633programming techniques employed by Erlang apply to AnyEvent::MP. Here is a
626sample: 634sample:
627 635
628 http://www.erlang.se/doc/programming_rules.shtml 636 http://www.Erlang.se/doc/programming_rules.shtml
629 http://erlang.org/doc/getting_started/part_frame.html # chapters 3 and 4 637 http://Erlang.org/doc/getting_started/part_frame.html # chapters 3 and 4
630 http://erlang.org/download/erlang-book-part1.pdf # chapters 5 and 6 638 http://Erlang.org/download/Erlang-book-part1.pdf # chapters 5 and 6
631 http://erlang.org/download/armstrong_thesis_2003.pdf # chapters 4 and 5 639 http://Erlang.org/download/armstrong_thesis_2003.pdf # chapters 4 and 5
632 640
633Despite the similarities, there are also some important differences: 641Despite the similarities, there are also some important differences:
634 642
635=over 4 643=over 4
636 644
647 655
648Erlang uses processes that selctively receive messages, and therefore 656Erlang uses processes that selctively receive messages, and therefore
649needs a queue. AEMP is event based, queuing messages would serve no useful 657needs a queue. AEMP is event based, queuing messages would serve no useful
650purpose. 658purpose.
651 659
652(But see L<Coro::MP> for a more erlang-like process model on top of AEMP). 660(But see L<Coro::MP> for a more Erlang-like process model on top of AEMP).
653 661
654=item * Erlang sends are synchronous, AEMP sends are asynchronous. 662=item * Erlang sends are synchronous, AEMP sends are asynchronous.
655 663
656Sending messages in erlang is synchronous and blocks the process. AEMP 664Sending messages in Erlang is synchronous and blocks the process. AEMP
657sends are immediate, connection establishment is handled in the 665sends are immediate, connection establishment is handled in the
658background. 666background.
659 667
660=item * Erlang can silently lose messages, AEMP cannot. 668=item * Erlang can silently lose messages, AEMP cannot.
661 669
664and c, and the other side only receives messages a and c). 672and c, and the other side only receives messages a and c).
665 673
666AEMP guarantees correct ordering, and the guarantee that there are no 674AEMP guarantees correct ordering, and the guarantee that there are no
667holes in the message sequence. 675holes in the message sequence.
668 676
669=item * In erlang, processes can be declared dead and later be found to be 677=item * In Erlang, processes can be declared dead and later be found to be
670alive. 678alive.
671 679
672In erlang it can happen that a monitored process is declared dead and 680In Erlang it can happen that a monitored process is declared dead and
673linked processes get killed, but later it turns out that the process is 681linked processes get killed, but later it turns out that the process is
674still alive - and can receive messages. 682still alive - and can receive messages.
675 683
676In AEMP, when port monitoring detects a port as dead, then that port will 684In AEMP, when port monitoring detects a port as dead, then that port will
677eventually be killed - it cannot happen that a node detects a port as dead 685eventually be killed - it cannot happen that a node detects a port as dead
678and then later sends messages to it, finding it is still alive. 686and then later sends messages to it, finding it is still alive.
679 687
680=item * Erlang can send messages to the wrong port, AEMP does not. 688=item * Erlang can send messages to the wrong port, AEMP does not.
681 689
682In erlang it is quite possible that a node that restarts reuses a process 690In Erlang it is quite possible that a node that restarts reuses a process
683ID known to other nodes for a completely different process, causing 691ID known to other nodes for a completely different process, causing
684messages destined for that process to end up in an unrelated process. 692messages destined for that process to end up in an unrelated process.
685 693
686AEMP never reuses port IDs, so old messages or old port IDs floating 694AEMP never reuses port IDs, so old messages or old port IDs floating
687around in the network will not be sent to an unrelated port. 695around in the network will not be sent to an unrelated port.
693securely authenticate nodes. 701securely authenticate nodes.
694 702
695=item * The AEMP protocol is optimised for both text-based and binary 703=item * The AEMP protocol is optimised for both text-based and binary
696communications. 704communications.
697 705
698The AEMP protocol, unlike the erlang protocol, supports both 706The AEMP protocol, unlike the Erlang protocol, supports both
699language-independent text-only protocols (good for debugging) and binary, 707language-independent text-only protocols (good for debugging) and binary,
700language-specific serialisers (e.g. Storable). 708language-specific serialisers (e.g. Storable).
701 709
702It has also been carefully designed to be implementable in other languages 710It has also been carefully designed to be implementable in other languages
703with a minimum of work while gracefully degrading fucntionality to make the 711with a minimum of work while gracefully degrading fucntionality to make the
704protocol simple. 712protocol simple.
705 713
714=item * AEMP has more flexible monitoring options than Erlang.
715
716In Erlang, you can chose to receive I<all> exit signals as messages
717or I<none>, there is no in-between, so monitoring single processes is
718difficult to implement. Monitoring in AEMP is more flexible than in
719Erlang, as one can choose between automatic kill, exit message or callback
720on a per-process basis.
721
722=item * Erlang has different semantics for monitoring and linking, AEMP has the same.
723
724Monitoring in Erlang is not an indicator of process death/crashes,
725as linking is (except linking is unreliable in Erlang). In AEMP, the
726semantics of monitoring and linking are identical, linking is simply
727two-way monitoring with automatic kill.
728
706=back 729=back
707 730
708=head1 SEE ALSO 731=head1 SEE ALSO
709 732
710L<AnyEvent>. 733L<AnyEvent>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines