… | |
… | |
817 | Even if not used otherwise, the socket can be a good indicator for the |
817 | Even if not used otherwise, the socket can be a good indicator for the |
818 | existence of the process - if the other process exits, you get a readable |
818 | existence of the process - if the other process exits, you get a readable |
819 | event on it, because exiting the process closes the socket (if it didn't |
819 | event on it, because exiting the process closes the socket (if it didn't |
820 | create any children using fork). |
820 | create any children using fork). |
821 | |
821 | |
|
|
822 | =over 4 |
|
|
823 | |
|
|
824 | =item Compatibility to L<AnyEvent::Fork::Remote> |
|
|
825 | |
|
|
826 | If you want to write code that works with both this module and |
|
|
827 | L<AnyEvent::Fork::Remote>, you need to write your code so that it assumes |
|
|
828 | there are two file handles for communications, which might not be unix |
|
|
829 | domain sockets. The C<run> function should start like this: |
|
|
830 | |
|
|
831 | sub run { |
|
|
832 | my ($rfh, @args) = @_; # @args is your normal arguments |
|
|
833 | my $wfh = fileno $rfh ? $rfh : *STDOUT; |
|
|
834 | |
|
|
835 | # now use $rfh for reading and $wfh for writing |
|
|
836 | } |
|
|
837 | |
|
|
838 | This checks whether the passed file handle is, in fact, the process |
|
|
839 | C<STDIN> handle. If it is, then the function was invoked visa |
|
|
840 | L<AnyEvent::Fork::Remote>, so STDIN should be used for reading and |
|
|
841 | C<STDOUT> should be used for writing. |
|
|
842 | |
|
|
843 | In all other cases, the function was called via this module, and there is |
|
|
844 | only one file handle that should be sued for reading and writing. |
|
|
845 | |
|
|
846 | =back |
|
|
847 | |
822 | Example: create a template for a process pool, pass a few strings, some |
848 | Example: create a template for a process pool, pass a few strings, some |
823 | file handles, then fork, pass one more string, and run some code. |
849 | file handles, then fork, pass one more string, and run some code. |
824 | |
850 | |
825 | my $pool = AnyEvent::Fork |
851 | my $pool = AnyEvent::Fork |
826 | ->new |
852 | ->new |
… | |
… | |
873 | the communications socket. |
899 | the communications socket. |
874 | |
900 | |
875 | The process object becomes unusable on return from this function - any |
901 | The process object becomes unusable on return from this function - any |
876 | further method calls result in undefined behaviour. |
902 | further method calls result in undefined behaviour. |
877 | |
903 | |
878 | The point of this method is to give you a file handle thta you cna pass |
904 | The point of this method is to give you a file handle that you can pass |
879 | to another process. In that other process, you can call C<new_from_fh |
905 | to another process. In that other process, you can call C<new_from_fh |
880 | AnyEvent::Fork> to create a new C<AnyEvent::Fork> object from it, thereby |
906 | AnyEvent::Fork $fh> to create a new C<AnyEvent::Fork> object from it, |
881 | effectively passing a fork object to another process. |
907 | thereby effectively passing a fork object to another process. |
882 | |
908 | |
883 | =cut |
909 | =cut |
884 | |
910 | |
885 | sub to_fh { |
911 | sub to_fh { |
886 | my ($self, $cb) = @_; |
912 | my ($self, $cb) = @_; |