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

Comparing AnyEvent-MP/MP/Intro.pod (file contents):
Revision 1.40 by root, Sat Sep 5 21:16:59 2009 UTC vs.
Revision 1.41 by root, Tue Oct 6 18:38:25 2009 UTC

477still be there, by recovering and restarting the service. 477still be there, by recovering and restarting the service.
478 478
479AnyEvent::MP supports this by catching exceptions and network problems, 479AnyEvent::MP supports this by catching exceptions and network problems,
480and notifying interested parties of this. 480and notifying interested parties of this.
481 481
482=head2 Exceptions, Network Errors and Monitors 482=head2 Exceptions, Port Context, Network Errors and Monitors
483 483
484=head3 Exceptions 484=head3 Exceptions
485 485
486Exceptions are handled on a per-port basis: receive callbacks are executed 486Exceptions are handled on a per-port basis: receive callbacks are executed
487in a special context, the port-context, and code that throws an uncaught 487in a special context, the so-called I<port-context>: code that throws an
488exception will cause the port to be C<kil>led. Killed ports are destroyed 488otherwise uncaught exception will cause the port to be C<kil>led. Killed
489automatically (killing ports is the only way to free ports, incidentally). 489ports are destroyed automatically (killing ports is the only way to free
490ports, incidentally).
490 491
491Ports can be monitored, even from a different host, and when a port is 492Ports can be monitored, even from a different host, and when a port is
492killed any entity monitoring it will be notified. 493killed any entity monitoring it will be notified.
493 494
494Here is a simple example: 495Here is a simple example:
575 }; 576 };
576 577
577Then you will find it does not work - when the after callback is executed, 578Then you will find it does not work - when the after callback is executed,
578it does not run in port context anymore, so exceptions will not be caught. 579it does not run in port context anymore, so exceptions will not be caught.
579 580
580For these cases, AnyEvent::MP exports a special "close constructor" called 581For these cases, AnyEvent::MP exports a special "closure constructor"
581C<psub>, which works just like perl's builtin C<sub>: 582called C<psub>, which works just like perl's builtin C<sub>:
582 583
583 my $port = port { 584 my $port = port {
584 after 1, psub { die "oops" }; 585 after 1, psub { die "oops" };
585 }; 586 };
586 587
587C<psub> stores C<$SELF> and returns a code reference. When the code 588C<psub> stores C<$SELF> and returns a code reference. When the code
588reference is invoked, it will run the code block within the context of 589reference is invoked, it will run the code block within the context of
589that port, so exception handling once more works as expected. 590that port, so exception handling once more works as expected.
591
592There is also a way to temporarily execute code in the context of some
593port, namely C<peval>:
594
595 peval $port, sub {
596 # die'ing here will kil $port
597 };
598
599The C<peval> function temporarily replaces C<$SELF> by the given C<$port>
600and then executes the given sub in a port context.
590 601
591=head3 Network Errors and the AEMP Guarantee 602=head3 Network Errors and the AEMP Guarantee
592 603
593I mentioned another important source of monitoring failures: network 604I mentioned another important source of monitoring failures: network
594problems. When a node loses connection to another node, it will invoke all 605problems. When a node loses connection to another node, it will invoke all

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines