… | |
… | |
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 | |
30 | This module (-family) implements a simple message passing framework. |
38 | This 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 | |
623 | AnyEvent::MP got lots of its ideas from distributed erlang (erlang node |
631 | AnyEvent::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 |
625 | programming techniques employed by erlang apply to AnyEvent::MP. Here is a |
633 | programming techniques employed by Erlang apply to AnyEvent::MP. Here is a |
626 | sample: |
634 | sample: |
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 | |
633 | Despite the similarities, there are also some important differences: |
641 | Despite the similarities, there are also some important differences: |
634 | |
642 | |
635 | =over 4 |
643 | =over 4 |
636 | |
644 | |
… | |
… | |
647 | |
655 | |
648 | Erlang uses processes that selctively receive messages, and therefore |
656 | Erlang uses processes that selctively receive messages, and therefore |
649 | needs a queue. AEMP is event based, queuing messages would serve no useful |
657 | needs a queue. AEMP is event based, queuing messages would serve no useful |
650 | purpose. |
658 | purpose. |
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 | |
656 | Sending messages in erlang is synchronous and blocks the process. AEMP |
664 | Sending messages in Erlang is synchronous and blocks the process. AEMP |
657 | sends are immediate, connection establishment is handled in the |
665 | sends are immediate, connection establishment is handled in the |
658 | background. |
666 | background. |
659 | |
667 | |
660 | =item * Erlang can silently lose messages, AEMP cannot. |
668 | =item * Erlang can silently lose messages, AEMP cannot. |
661 | |
669 | |
… | |
… | |
664 | and c, and the other side only receives messages a and c). |
672 | and c, and the other side only receives messages a and c). |
665 | |
673 | |
666 | AEMP guarantees correct ordering, and the guarantee that there are no |
674 | AEMP guarantees correct ordering, and the guarantee that there are no |
667 | holes in the message sequence. |
675 | holes 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 |
670 | alive. |
678 | alive. |
671 | |
679 | |
672 | In erlang it can happen that a monitored process is declared dead and |
680 | In Erlang it can happen that a monitored process is declared dead and |
673 | linked processes get killed, but later it turns out that the process is |
681 | linked processes get killed, but later it turns out that the process is |
674 | still alive - and can receive messages. |
682 | still alive - and can receive messages. |
675 | |
683 | |
676 | In AEMP, when port monitoring detects a port as dead, then that port will |
684 | In AEMP, when port monitoring detects a port as dead, then that port will |
677 | eventually be killed - it cannot happen that a node detects a port as dead |
685 | eventually be killed - it cannot happen that a node detects a port as dead |
678 | and then later sends messages to it, finding it is still alive. |
686 | and 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 | |
682 | In erlang it is quite possible that a node that restarts reuses a process |
690 | In Erlang it is quite possible that a node that restarts reuses a process |
683 | ID known to other nodes for a completely different process, causing |
691 | ID known to other nodes for a completely different process, causing |
684 | messages destined for that process to end up in an unrelated process. |
692 | messages destined for that process to end up in an unrelated process. |
685 | |
693 | |
686 | AEMP never reuses port IDs, so old messages or old port IDs floating |
694 | AEMP never reuses port IDs, so old messages or old port IDs floating |
687 | around in the network will not be sent to an unrelated port. |
695 | around in the network will not be sent to an unrelated port. |
… | |
… | |
693 | securely authenticate nodes. |
701 | securely 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 |
696 | communications. |
704 | communications. |
697 | |
705 | |
698 | The AEMP protocol, unlike the erlang protocol, supports both |
706 | The AEMP protocol, unlike the Erlang protocol, supports both |
699 | language-independent text-only protocols (good for debugging) and binary, |
707 | language-independent text-only protocols (good for debugging) and binary, |
700 | language-specific serialisers (e.g. Storable). |
708 | language-specific serialisers (e.g. Storable). |
701 | |
709 | |
702 | It has also been carefully designed to be implementable in other languages |
710 | It has also been carefully designed to be implementable in other languages |
703 | with a minimum of work while gracefully degrading fucntionality to make the |
711 | with a minimum of work while gracefully degrading fucntionality to make the |
704 | protocol simple. |
712 | protocol simple. |
705 | |
713 | |
|
|
714 | =item * AEMP has more flexible monitoring options than Erlang. |
|
|
715 | |
|
|
716 | In Erlang, you can chose to receive I<all> exit signals as messages |
|
|
717 | or I<none>, there is no in-between, so monitoring single processes is |
|
|
718 | difficult to implement. Monitoring in AEMP is more flexible than in |
|
|
719 | Erlang, as one can choose between automatic kill, exit message or callback |
|
|
720 | on a per-process basis. |
|
|
721 | |
|
|
722 | =item * Erlang has different semantics for monitoring and linking, AEMP has the same. |
|
|
723 | |
|
|
724 | Monitoring in Erlang is not an indicator of process death/crashes, |
|
|
725 | as linking is (except linking is unreliable in Erlang). In AEMP, the |
|
|
726 | semantics of monitoring and linking are identical, linking is simply |
|
|
727 | two-way monitoring with automatic kill. |
|
|
728 | |
706 | =back |
729 | =back |
707 | |
730 | |
708 | =head1 SEE ALSO |
731 | =head1 SEE ALSO |
709 | |
732 | |
710 | L<AnyEvent>. |
733 | L<AnyEvent>. |