… | |
… | |
621 | the package, then the package above the package and so on (e.g. |
621 | the package, then the package above the package and so on (e.g. |
622 | C<MyApp::Chat::Server>, C<MyApp::Chat>, C<MyApp>) until the function |
622 | C<MyApp::Chat::Server>, C<MyApp::Chat>, C<MyApp>) until the function |
623 | exists or it runs out of package names. |
623 | exists or it runs out of package names. |
624 | |
624 | |
625 | The init function is then called with the newly-created port as context |
625 | The init function is then called with the newly-created port as context |
626 | object (C<$SELF>) and the C<@initdata> values as arguments. |
626 | object (C<$SELF>) and the C<@initdata> values as arguments. It I<must> |
|
|
627 | call one of the C<rcv> functions to set callbacks on C<$SELF>, otherwise |
|
|
628 | the port might not get created. |
627 | |
629 | |
628 | A common idiom is to pass a local port, immediately monitor the spawned |
630 | A common idiom is to pass a local port, immediately monitor the spawned |
629 | port, and in the remote init function, immediately monitor the passed |
631 | port, and in the remote init function, immediately monitor the passed |
630 | local port. This two-way monitoring ensures that both ports get cleaned up |
632 | local port. This two-way monitoring ensures that both ports get cleaned up |
631 | when there is a problem. |
633 | when there is a problem. |
632 | |
634 | |
|
|
635 | C<spawn> guarantees that the C<$initfunc> has no visible effects on the |
|
|
636 | caller before C<spawn> returns (by delaying invocation when spawn is |
|
|
637 | called for the local node). |
|
|
638 | |
633 | Example: spawn a chat server port on C<$othernode>. |
639 | Example: spawn a chat server port on C<$othernode>. |
634 | |
640 | |
635 | # this node, executed from within a port context: |
641 | # this node, executed from within a port context: |
636 | my $server = spawn $othernode, "MyApp::Chat::Server::connect", $SELF; |
642 | my $server = spawn $othernode, "MyApp::Chat::Server::connect", $SELF; |
637 | mon $server; |
643 | mon $server; |
… | |
… | |
651 | |
657 | |
652 | sub _spawn { |
658 | sub _spawn { |
653 | my $port = shift; |
659 | my $port = shift; |
654 | my $init = shift; |
660 | my $init = shift; |
655 | |
661 | |
|
|
662 | # rcv will create the actual port |
656 | local $SELF = "$NODE#$port"; |
663 | local $SELF = "$NODE#$port"; |
657 | eval { |
664 | eval { |
658 | &{ load_func $init } |
665 | &{ load_func $init } |
659 | }; |
666 | }; |
660 | _self_die if $@; |
667 | _self_die if $@; |
… | |
… | |
856 | L<AnyEvent::MP::Kernel> - more, lower-level, stuff. |
863 | L<AnyEvent::MP::Kernel> - more, lower-level, stuff. |
857 | |
864 | |
858 | L<AnyEvent::MP::Global> - network maintainance and port groups, to find |
865 | L<AnyEvent::MP::Global> - network maintainance and port groups, to find |
859 | your applications. |
866 | your applications. |
860 | |
867 | |
|
|
868 | L<AnyEvent::MP::LogCatcher> - simple service to display log messages from |
|
|
869 | all nodes. |
|
|
870 | |
861 | L<AnyEvent>. |
871 | L<AnyEvent>. |
862 | |
872 | |
863 | =head1 AUTHOR |
873 | =head1 AUTHOR |
864 | |
874 | |
865 | Marc Lehmann <schmorp@schmorp.de> |
875 | Marc Lehmann <schmorp@schmorp.de> |