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.36 by root, Thu Aug 6 10:46:48 2009 UTC vs.
Revision 1.37 by root, Fri Aug 7 16:47:23 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 27
31 # monitoring 28 # monitoring
32 mon $port, $cb->(@msg) # callback is invoked on death 29 mon $port, $cb->(@msg) # callback is invoked on death
33 mon $port, $otherport # kill otherport on abnormal death 30 mon $port, $otherport # kill otherport on abnormal death
34 mon $port, $otherport, @msg # send message on death 31 mon $port, $otherport, @msg # send message on death
513C<$rvport> defaults to C<$SELF>. 510C<$rvport> defaults to C<$SELF>.
514 511
515In the last form (message), a message of the form C<@msg, @reason> will be 512In the last form (message), a message of the form C<@msg, @reason> will be
516C<snd>. 513C<snd>.
517 514
515As a rule of thumb, monitoring requests should always monitor a port from
516a local port (or callback). The reason is that kill messages might get
517lost, just like any other message. Another less obvious reason is that
518even monitoring requests can get lost (for exmaple, when the connection
519to the other node goes down permanently). When monitoring a port locally
520these problems do not exist.
521
518Example: call a given callback when C<$port> is killed. 522Example: call a given callback when C<$port> is killed.
519 523
520 mon $port, sub { warn "port died because of <@_>\n" }; 524 mon $port, sub { warn "port died because of <@_>\n" };
521 525
522Example: kill ourselves when C<$port> is killed abnormally. 526Example: kill ourselves when C<$port> is killed abnormally.
578 #TODO: mon-less form? 582 #TODO: mon-less form?
579 583
580 mon $port, sub { 0 && @refs } 584 mon $port, sub { 0 && @refs }
581} 585}
582 586
583=item lnk $port1, $port2
584
585=item lnk $otherport
586
587Link two ports. This is simply a shorthand for:
588
589 mon $port1, $port2;
590 mon $port2, $port1;
591
592It means that if either one is killed abnormally, the other one gets
593killed as well.
594
595The one-argument form assumes that one port is C<$SELF>.
596
597=cut
598
599sub lnk {
600 my $port1 = shift;
601 my $port2 = @_ ? shift : $SELF || Carp::croak 'lnk: called with one argument only, but $SELF not set,';
602
603 mon $port1, $port2;
604 mon $port2, $port1;
605}
606
607=item kil $port[, @reason] 587=item kil $port[, @reason]
608 588
609Kill the specified port with the given C<@reason>. 589Kill the specified port with the given C<@reason>.
610 590
611If no C<@reason> is specified, then the port is killed "normally" (linked 591If no C<@reason> is specified, then the port is killed "normally" (linked
759or I<none>, there is no in-between, so monitoring single processes is 739or I<none>, there is no in-between, so monitoring single processes is
760difficult to implement. Monitoring in AEMP is more flexible than in 740difficult to implement. Monitoring in AEMP is more flexible than in
761Erlang, as one can choose between automatic kill, exit message or callback 741Erlang, as one can choose between automatic kill, exit message or callback
762on a per-process basis. 742on a per-process basis.
763 743
764=item * Erlang has different semantics for monitoring and linking, AEMP has the same. 744=item * Erlang tries to hide remote/local connections, AEMP does not.
765 745
766Monitoring in Erlang is not an indicator of process death/crashes, 746Monitoring in Erlang is not an indicator of process death/crashes,
767as linking is (except linking is unreliable in Erlang). In AEMP, the 747as linking is (except linking is unreliable in Erlang).
768semantics of monitoring and linking are identical, linking is simply 748
769two-way monitoring with automatic kill. 749In AEMP, you don't "look up" registered port names or send to named ports
750that might or might not be persistent. Instead, you normally spawn a port
751on the remote node. The init function monitors the you, and you monitor
752the remote port. Since both monitors are local to the node, they are much
753more reliable.
754
755This also saves round-trips and avoids sending messages to the wrong port
756(hard to do in Erlang).
770 757
771=back 758=back
772 759
773=head1 SEE ALSO 760=head1 SEE ALSO
774 761

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines