… | |
… | |
175 | you really I<are> done. |
175 | you really I<are> done. |
176 | |
176 | |
177 | =head2 Example 2: Asynchronous Backend |
177 | =head2 Example 2: Asynchronous Backend |
178 | |
178 | |
179 | This example implements multiple count-downs in the child, using |
179 | This example implements multiple count-downs in the child, using |
180 | L<AnyEvent> timers. While this is a bit silly (one could use timers in te |
180 | L<AnyEvent> timers. While this is a bit silly (one could use timers in the |
181 | parent just as well), it illustrates the ability to use AnyEvent in the |
181 | parent just as well), it illustrates the ability to use AnyEvent in the |
182 | child and the fact that responses can arrive in a different order then the |
182 | child and the fact that responses can arrive in a different order then the |
183 | requests. |
183 | requests. |
184 | |
184 | |
185 | It also shows how to embed the actual child code into a C<__DATA__> |
185 | It also shows how to embed the actual child code into a C<__DATA__> |
… | |
… | |
391 | use Errno (); |
391 | use Errno (); |
392 | use Guard (); |
392 | use Guard (); |
393 | |
393 | |
394 | use AnyEvent; |
394 | use AnyEvent; |
395 | |
395 | |
396 | our $VERSION = 1.1; |
396 | our $VERSION = 1.21; |
397 | |
397 | |
398 | =item my $rpc = AnyEvent::Fork::RPC::run $fork, $function, [key => value...] |
398 | =item my $rpc = AnyEvent::Fork::RPC::run $fork, $function, [key => value...] |
399 | |
399 | |
400 | The traditional way to call it. But it is way cooler to call it in the |
400 | The traditional way to call it. But it is way cooler to call it in the |
401 | following way: |
401 | following way: |
… | |
… | |
454 | It is called very early - before the serialisers are created or the |
454 | It is called very early - before the serialisers are created or the |
455 | C<$function> name is resolved into a function reference, so it could be |
455 | C<$function> name is resolved into a function reference, so it could be |
456 | used to load any modules that provide the serialiser or function. It can |
456 | used to load any modules that provide the serialiser or function. It can |
457 | not, however, create events. |
457 | not, however, create events. |
458 | |
458 | |
|
|
459 | =item done => $function (default C<CORE::exit>) |
|
|
460 | |
|
|
461 | The function to call when the asynchronous backend detects an end of file |
|
|
462 | condition when reading from the communications socket I<and> there are no |
|
|
463 | outstanding requests. It's ignored by the synchronous backend. |
|
|
464 | |
|
|
465 | By overriding this you can prolong the life of a RPC process after e.g. |
|
|
466 | the parent has exited by running the event loop in the provided function |
|
|
467 | (or simply calling it, for example, when your child process uses L<EV> you |
|
|
468 | could provide L<EV::loop> as C<done> function). |
|
|
469 | |
|
|
470 | Of course, in that case you are responsible for exiting at the appropriate |
|
|
471 | time and not returning from |
|
|
472 | |
459 | =item async => $boolean (default: 0) |
473 | =item async => $boolean (default: 0) |
460 | |
474 | |
461 | The default server used in the child does all I/O blockingly, and only |
475 | The default server used in the child does all I/O blockingly, and only |
462 | allows a single RPC call to execute concurrently. |
476 | allows a single RPC call to execute concurrently. |
463 | |
477 | |
… | |
… | |
511 | ( |
525 | ( |
512 | sub { pack "(w/a*)*", @_ }, |
526 | sub { pack "(w/a*)*", @_ }, |
513 | sub { unpack "(w/a*)*", shift } |
527 | sub { unpack "(w/a*)*", shift } |
514 | ) |
528 | ) |
515 | |
529 | |
|
|
530 | =item json - C<$AnyEvent::Fork::RPC::CBOR_XS_SERIALISER> |
|
|
531 | |
|
|
532 | This serialiser creates CBOR::XS arrays - you have to make sure the |
|
|
533 | L<CBOR::XS> module is installed for this serialiser to work. It can be |
|
|
534 | beneficial for sharing when you preload the L<CBOR::XS> module in a template |
|
|
535 | process. |
|
|
536 | |
|
|
537 | L<CBOR::XS> is about as fast as the octet string serialiser, but supports |
|
|
538 | complex data structures (similar to JSON) and is faster than any of the |
|
|
539 | other serialisers. If you have the L<CBOR::XS> module available, it's the |
|
|
540 | best choice. |
|
|
541 | |
|
|
542 | Note that the CBOR::XS module supports some extensions to encode cyclic |
|
|
543 | and self-referencing data structures, which are not enabled. You need to |
|
|
544 | write your own serialiser to take advantage of these. |
|
|
545 | |
|
|
546 | Implementation: |
|
|
547 | |
|
|
548 | use CBOR::XS (); |
|
|
549 | ( |
|
|
550 | sub { CBOR::XS::encode_cbor \@_ }, |
|
|
551 | sub { @{ CBOR::XS::decode_cbor shift } } |
|
|
552 | ) |
|
|
553 | |
516 | =item json - C<$AnyEvent::Fork::RPC::JSON_SERIALISER> |
554 | =item json - C<$AnyEvent::Fork::RPC::JSON_SERIALISER> |
517 | |
555 | |
518 | This serialiser creates JSON arrays - you have to make sure the L<JSON> |
556 | This serialiser creates JSON arrays - you have to make sure the L<JSON> |
519 | module is installed for this serialiser to work. It can be beneficial for |
557 | module is installed for this serialiser to work. It can be beneficial for |
520 | sharing when you preload the L<JSON> module in a template process. |
558 | sharing when you preload the L<JSON> module in a template process. |
… | |
… | |
569 | examples. |
607 | examples. |
570 | |
608 | |
571 | =cut |
609 | =cut |
572 | |
610 | |
573 | our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })'; |
611 | our $STRING_SERIALISER = '(sub { pack "(w/a*)*", @_ }, sub { unpack "(w/a*)*", shift })'; |
|
|
612 | our $CBOR_XS_SERIALISER = 'use CBOR::XS (); (sub { CBOR::XS::encode_cbor \@_ }, sub { @{ CBOR::XS::decode_cbor shift } })'; |
574 | our $JSON_SERIALISER = 'use JSON (); (sub { JSON::encode_json \@_ }, sub { @{ JSON::decode_json shift } })'; |
613 | our $JSON_SERIALISER = 'use JSON (); (sub { JSON::encode_json \@_ }, sub { @{ JSON::decode_json shift } })'; |
575 | our $STORABLE_SERIALISER = 'use Storable (); (sub { Storable::freeze \@_ }, sub { @{ Storable::thaw shift } })'; |
614 | our $STORABLE_SERIALISER = 'use Storable (); (sub { Storable::freeze \@_ }, sub { @{ Storable::thaw shift } })'; |
576 | our $NSTORABLE_SERIALISER = 'use Storable (); (sub { Storable::nfreeze \@_ }, sub { @{ Storable::thaw shift } })'; |
615 | our $NSTORABLE_SERIALISER = 'use Storable (); (sub { Storable::nfreeze \@_ }, sub { @{ Storable::thaw shift } })'; |
577 | |
616 | |
578 | sub run { |
617 | sub run { |
579 | my ($self, $function, %arg) = @_; |
618 | my ($self, $function, %arg) = @_; |
… | |
… | |
615 | }; |
654 | }; |
616 | |
655 | |
617 | my $module = "AnyEvent::Fork::RPC::" . ($arg{async} ? "Async" : "Sync"); |
656 | my $module = "AnyEvent::Fork::RPC::" . ($arg{async} ? "Async" : "Sync"); |
618 | |
657 | |
619 | $self->require ($module) |
658 | $self->require ($module) |
620 | ->send_arg ($function, $arg{init}, $serialiser) |
659 | ->send_arg ($function, $arg{init}, $serialiser, $arg{done} || "$module\::do_exit") |
621 | ->run ("$module\::run", sub { |
660 | ->run ("$module\::run", sub { |
622 | $fh = shift; |
661 | $fh = shift; |
623 | |
662 | |
624 | my ($id, $len); |
663 | my ($id, $len); |
625 | $rw = AE::io $fh, 0, sub { |
664 | $rw = AE::io $fh, 0, sub { |
… | |
… | |
741 | See the examples section earlier in this document for some actual |
780 | See the examples section earlier in this document for some actual |
742 | examples. |
781 | examples. |
743 | |
782 | |
744 | =back |
783 | =back |
745 | |
784 | |
|
|
785 | =head2 PROCESS EXIT |
|
|
786 | |
|
|
787 | If and when the child process exits depends on the backend and |
|
|
788 | configuration. Apart from explicit exits (e.g. by calling C<exit>) or |
|
|
789 | runtime conditions (uncaught exceptions, signals etc.), the backends exit |
|
|
790 | under these conditions: |
|
|
791 | |
|
|
792 | =over 4 |
|
|
793 | |
|
|
794 | =item Synchronous Backend |
|
|
795 | |
|
|
796 | The synchronous backend is very simple: when the process waits for another |
|
|
797 | request to arrive and the writing side (usually in the parent) is closed, |
|
|
798 | it will exit normally, i.e. as if your main program reached the end of the |
|
|
799 | file. |
|
|
800 | |
|
|
801 | That means that if your parent process exits, the RPC process will usually |
|
|
802 | exit as well, either because it is idle anyway, or because it executes a |
|
|
803 | request. In the latter case, you will likely get an error when the RPc |
|
|
804 | process tries to send the results to the parent (because agruably, you |
|
|
805 | shouldn't exit your parent while there are still outstanding requests). |
|
|
806 | |
|
|
807 | The process is usually quiescent when it happens, so it should rarely be a |
|
|
808 | problem, and C<END> handlers can be used to clean up. |
|
|
809 | |
|
|
810 | =item Asynchronous Backend |
|
|
811 | |
|
|
812 | For the asynchronous backend, things are more complicated: Whenever it |
|
|
813 | listens for another request by the parent, it might detect that the socket |
|
|
814 | was closed (e.g. because the parent exited). It will sotp listening for |
|
|
815 | new requests and instead try to write out any remaining data (if any) or |
|
|
816 | simply check whether the socket can be written to. After this, the RPC |
|
|
817 | process is effectively done - no new requests are incoming, no outstanding |
|
|
818 | request data can be written back. |
|
|
819 | |
|
|
820 | Since chances are high that there are event watchers that the RPC server |
|
|
821 | knows nothing about (why else would one use the async backend if not for |
|
|
822 | the ability to register watchers?), the event loop would often happily |
|
|
823 | continue. |
|
|
824 | |
|
|
825 | This is why the asynchronous backend explicitly calls C<CORE::exit> when |
|
|
826 | it is done (under other circumstances, such as when there is an I/O error |
|
|
827 | and there is outstanding data to write, it will log a fatal message via |
|
|
828 | L<AnyEvent::Log>, also causing the program to exit). |
|
|
829 | |
|
|
830 | You can override this by specifying a function name to call via the C<done> |
|
|
831 | parameter instead. |
|
|
832 | |
|
|
833 | =back |
|
|
834 | |
746 | =head1 ADVANCED TOPICS |
835 | =head1 ADVANCED TOPICS |
747 | |
836 | |
748 | =head2 Choosing a backend |
837 | =head2 Choosing a backend |
749 | |
838 | |
750 | So how do you decide which backend to use? Well, that's your problem to |
839 | So how do you decide which backend to use? Well, that's your problem to |