1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | AnyEvent::Fork::RPC - simple RPC extension for AnyEvent::Fork |
3 | AnyEvent::Fork::RPC - simple RPC extension for AnyEvent::Fork |
4 | |
4 | |
5 | THE API IS NOT FINISHED, CONSIDER THIS A TECHNOLOGY DEMO |
5 | THE API IS NOT FINISHED, CONSIDER THIS A BETA RELEASE |
6 | |
6 | |
7 | =head1 SYNOPSIS |
7 | =head1 SYNOPSIS |
8 | |
8 | |
9 | use AnyEvent::Fork::RPC; |
9 | use AnyEvent::Fork::RPC; |
10 | # use AnyEvent::Fork is not needed |
10 | # use AnyEvent::Fork is not needed |
… | |
… | |
305 | |
305 | |
306 | use Errno (); |
306 | use Errno (); |
307 | use Guard (); |
307 | use Guard (); |
308 | |
308 | |
309 | use AnyEvent; |
309 | use AnyEvent; |
|
|
310 | # explicit version on next line, as some cpan-testers test with the 0.1 version, |
|
|
311 | # ignoring dependencies, and this line will at least give a clear indication of that. |
310 | use AnyEvent::Fork; # we don't actually depend on it, this is for convenience |
312 | use AnyEvent::Fork 0.6; # we don't actually depend on it, this is for convenience |
311 | |
313 | |
312 | our $VERSION = 0.1; |
314 | our $VERSION = 0.2; |
313 | |
315 | |
314 | =item my $rpc = AnyEvent::Fork::RPC::run $fork, $function, [key => value...] |
316 | =item my $rpc = AnyEvent::Fork::RPC::run $fork, $function, [key => value...] |
315 | |
317 | |
316 | The traditional way to call it. But it is way cooler to call it in the |
318 | The traditional way to call it. But it is way cooler to call it in the |
317 | following way: |
319 | following way: |
… | |
… | |
550 | undef $rw; undef $ww; # it ends here |
552 | undef $rw; undef $ww; # it ends here |
551 | |
553 | |
552 | if (@rcb || %rcb) { |
554 | if (@rcb || %rcb) { |
553 | $on_error->("unexpected eof"); |
555 | $on_error->("unexpected eof"); |
554 | } else { |
556 | } else { |
555 | $on_destroy->(); |
557 | $on_destroy->() |
|
|
558 | if $on_destroy; |
556 | } |
559 | } |
557 | } elsif ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) { |
560 | } elsif ($! != Errno::EAGAIN && $! != Errno::EWOULDBLOCK) { |
558 | undef $rw; undef $ww; # it ends here |
561 | undef $rw; undef $ww; # it ends here |
559 | $on_error->("read: $!"); |
562 | $on_error->("read: $!"); |
560 | } |
563 | } |
… | |
… | |
564 | }); |
567 | }); |
565 | |
568 | |
566 | my $guard = Guard::guard { |
569 | my $guard = Guard::guard { |
567 | $shutdown = 1; |
570 | $shutdown = 1; |
568 | |
571 | |
569 | $wcb->() if $fh && !$ww; |
572 | shutdown $fh, 1 if $fh && !$ww; |
570 | }; |
573 | }; |
571 | |
574 | |
572 | my $id; |
575 | my $id; |
573 | |
576 | |
574 | $arg{async} |
577 | $arg{async} |
… | |
… | |
576 | $id = ($id == 0xffffffff ? 0 : $id) + 1; |
579 | $id = ($id == 0xffffffff ? 0 : $id) + 1; |
577 | $id = ($id == 0xffffffff ? 0 : $id) + 1 while exists $rcb{$id}; # rarely loops |
580 | $id = ($id == 0xffffffff ? 0 : $id) + 1 while exists $rcb{$id}; # rarely loops |
578 | |
581 | |
579 | $rcb{$id} = pop; |
582 | $rcb{$id} = pop; |
580 | |
583 | |
581 | $guard; # keep it alive |
584 | $guard if 0; # keep it alive |
582 | |
585 | |
583 | $wbuf .= pack "LL/a*", $id, &$f; |
586 | $wbuf .= pack "LL/a*", $id, &$f; |
584 | $ww ||= $fh && AE::io $fh, 1, $wcb; |
587 | $ww ||= $fh && AE::io $fh, 1, $wcb; |
585 | } |
588 | } |
586 | : sub { |
589 | : sub { |
… | |
… | |
772 | |
775 | |
773 | Of course, this might be blocking if you pass a lot of file descriptors, |
776 | Of course, this might be blocking if you pass a lot of file descriptors, |
774 | so you might want to look into L<AnyEvent::FDpasser> which can handle the |
777 | so you might want to look into L<AnyEvent::FDpasser> which can handle the |
775 | gory details. |
778 | gory details. |
776 | |
779 | |
|
|
780 | =head1 EXCEPTIONS |
|
|
781 | |
|
|
782 | There are no provisions whatsoever for catching exceptions at this time - |
|
|
783 | in the child, exeptions might kill the process, causing calls to be lost |
|
|
784 | and the parent encountering a fatal error. In the parent, exceptions in |
|
|
785 | the result callback will not be caught and cause undefined behaviour. |
|
|
786 | |
777 | =head1 SEE ALSO |
787 | =head1 SEE ALSO |
778 | |
788 | |
779 | L<AnyEvent::Fork>, to create the processes in the first place. |
789 | L<AnyEvent::Fork>, to create the processes in the first place. |
780 | |
790 | |
781 | L<AnyEvent::Fork::Pool>, to manage whole pools of processes. |
791 | L<AnyEvent::Fork::Pool>, to manage whole pools of processes. |