ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-Fork-RPC/RPC.pm
(Generate patch)

Comparing AnyEvent-Fork-RPC/RPC.pm (file contents):
Revision 1.45 by root, Thu Sep 12 15:15:49 2019 UTC vs.
Revision 1.46 by root, Sun Sep 15 20:18:14 2019 UTC

391use Errno (); 391use Errno ();
392use Guard (); 392use Guard ();
393 393
394use AnyEvent; 394use AnyEvent;
395 395
396our $VERSION = 1.25; 396our $VERSION = '2.0';
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
400The traditional way to call it. But it is way cooler to call it in the 400The traditional way to call it. But it is way cooler to call it in the
401following way: 401following way:
458 458
459=item done => $function (default: C<CORE::exit>) 459=item done => $function (default: C<CORE::exit>)
460 460
461The function to call when the asynchronous backend detects an end of file 461The function to call when the asynchronous backend detects an end of file
462condition when reading from the communications socket I<and> there are no 462condition when reading from the communications socket I<and> there are no
463outstanding requests. It's ignored by the synchronous backend. 463outstanding requests. It is ignored by the synchronous backend.
464 464
465By overriding this you can prolong the life of a RPC process after e.g. 465By overriding this you can prolong the life of a RPC process after e.g.
466the parent has exited by running the event loop in the provided function 466the 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 467(or simply calling it, for example, when your child process uses L<EV> you
468could provide L<EV::run> as C<done> function). 468could provide L<EV::run> as C<done> function).
801The following function is not available in this module. They are only 801The following function is not available in this module. They are only
802available in the namespace of this module when the child is running, 802available in the namespace of this module when the child is running,
803without having to load any extra modules. They are part of the child-side 803without having to load any extra modules. They are part of the child-side
804API of L<AnyEvent::Fork::RPC>. 804API of L<AnyEvent::Fork::RPC>.
805 805
806Note that these functions are typically not yet declared when code is
807compiled into the child, because the backend module is only loaded when
808you call C<run>, which is typically the last method you call on the fork
809object.
810
811Therefore, you either have to explicitly pre-load the right backend module
812or mark calls to these functions as function calls, e.g.:
813
814 AnyEvent::Fork::RPC::event (0 => "five");
815 AnyEvent::Fork::RPC::event->(0 => "five");
816 &AnyEvent::Fork::RPC::flush;
817
806=over 4 818=over 4
807 819
808=item AnyEvent::Fork::RPC::event ... 820=item AnyEvent::Fork::RPC::event (...)
809 821
810Send an event to the parent. Events are a bit like RPC calls made by the 822Send an event to the parent. Events are a bit like RPC calls made by the
811child process to the parent, except that there is no notion of return 823child process to the parent, except that there is no notion of return
812values. 824values.
813 825
816 828
817Note: the event data, like any data send to the parent, might not be sent 829Note: the event data, like any data send to the parent, might not be sent
818immediatelly but queued for later sending, so there is no guarantee that 830immediatelly but queued for later sending, so there is no guarantee that
819the event has been sent to the parent when the call returns - when you 831the event has been sent to the parent when the call returns - when you
820e.g. exit directly after calling this function, the parent might never 832e.g. exit directly after calling this function, the parent might never
821receive the event. 833receive the event. See the next function for a remedy.
834
835=item $success = AnyEvent::Fork::RPC::flush ()
836
837Synchronously wait and flush the reply data to the parent. Returns true on
838success and false otherwise (i.e. when the reply data cannot be written at
839all). Ignoring the success status is a common and healthy behaviour.
840
841Only the "async" backend does something on C<flush> - the "sync" backend
842is not buffering reply data and always returns true from this function.
843
844Normally, reply data might or might not be written to the parent
845immediatelly but is buffered. This can greatly improve performance and
846efficiency, but sometimes can get in your way: for example. when you want
847to send an error message just before exiting, or when you want to ensure
848replies timely reach the parent before starting a long blocking operation.
849
850In these cases, you can call this function to flush any outstanding reply
851data to the parent. This is done blockingly, so no requests will be
852handled and no event callbacks will be called.
853
854For example, you could wrap your request function in a C<eval> block and
855report the exception string back to the caller just before exiting:
856
857 sub req {
858 ...
859
860 eval {
861 ...
862 };
863
864 if ($@) {
865 AnyEvent::RPC::event (throw => "$@");
866 AnyEvent::RPC::flush ();
867 exit;
868 }
869
870 ...
871 }
822 872
823=back 873=back
824 874
825=head2 PROCESS EXIT 875=head2 PROCESS EXIT
826 876

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines