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