… | |
… | |
668 | } |
668 | } |
669 | |
669 | |
670 | =item $pid = $proc->pid |
670 | =item $pid = $proc->pid |
671 | |
671 | |
672 | Returns the process id of the process I<iff it is a direct child of the |
672 | Returns the process id of the process I<iff it is a direct child of the |
673 | process running AnyEvent::Fork>, and C<undef> otherwise. |
673 | process running AnyEvent::Fork>, and C<undef> otherwise. As a general |
|
|
674 | rule (that you cannot rely upon), processes created via C<new_exec>, |
|
|
675 | L<AnyEvent::Fork::Early> or L<AnyEvent::Fork::Template> are direct |
|
|
676 | children, while all other processes are not. |
674 | |
677 | |
675 | Normally, only processes created via C<< AnyEvent::Fork->new_exec >> and |
678 | Or in other words, you do not normally have to take care of zombies for |
676 | L<AnyEvent::Fork::Template> are direct children, and you are responsible |
679 | processes created via C<new>, but when in doubt, or zombies are a problem, |
677 | to clean up their zombies when they die. |
680 | you need to check whether a process is a diretc child by calling this |
678 | |
681 | method, and possibly creating a child watcher or reap it manually. |
679 | All other processes are not direct children, and will be cleaned up by |
|
|
680 | AnyEvent::Fork itself. |
|
|
681 | |
682 | |
682 | =cut |
683 | =cut |
683 | |
684 | |
684 | sub pid { |
685 | sub pid { |
685 | $_[0][PID] |
686 | $_[0][PID] |
… | |
… | |
816 | |
817 | |
817 | Even if not used otherwise, the socket can be a good indicator for the |
818 | 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 |
819 | 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 |
820 | event on it, because exiting the process closes the socket (if it didn't |
820 | create any children using fork). |
821 | create any children using fork). |
|
|
822 | |
|
|
823 | =over 4 |
|
|
824 | |
|
|
825 | =item Compatibility to L<AnyEvent::Fork::Remote> |
|
|
826 | |
|
|
827 | If you want to write code that works with both this module and |
|
|
828 | L<AnyEvent::Fork::Remote>, you need to write your code so that it assumes |
|
|
829 | there are two file handles for communications, which might not be unix |
|
|
830 | domain sockets. The C<run> function should start like this: |
|
|
831 | |
|
|
832 | sub run { |
|
|
833 | my ($rfh, @args) = @_; # @args is your normal arguments |
|
|
834 | my $wfh = fileno $rfh ? $rfh : *STDOUT; |
|
|
835 | |
|
|
836 | # now use $rfh for reading and $wfh for writing |
|
|
837 | } |
|
|
838 | |
|
|
839 | This checks whether the passed file handle is, in fact, the process |
|
|
840 | C<STDIN> handle. If it is, then the function was invoked visa |
|
|
841 | L<AnyEvent::Fork::Remote>, so STDIN should be used for reading and |
|
|
842 | C<STDOUT> should be used for writing. |
|
|
843 | |
|
|
844 | In all other cases, the function was called via this module, and there is |
|
|
845 | only one file handle that should be sued for reading and writing. |
|
|
846 | |
|
|
847 | =back |
821 | |
848 | |
822 | Example: create a template for a process pool, pass a few strings, some |
849 | 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. |
850 | file handles, then fork, pass one more string, and run some code. |
824 | |
851 | |
825 | my $pool = AnyEvent::Fork |
852 | my $pool = AnyEvent::Fork |
… | |
… | |
873 | the communications socket. |
900 | the communications socket. |
874 | |
901 | |
875 | The process object becomes unusable on return from this function - any |
902 | The process object becomes unusable on return from this function - any |
876 | further method calls result in undefined behaviour. |
903 | further method calls result in undefined behaviour. |
877 | |
904 | |
878 | The point of this method is to give you a file handle thta you cna pass |
905 | 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 |
906 | 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 |
907 | AnyEvent::Fork $fh> to create a new C<AnyEvent::Fork> object from it, |
881 | effectively passing a fork object to another process. |
908 | thereby effectively passing a fork object to another process. |
882 | |
909 | |
883 | =cut |
910 | =cut |
884 | |
911 | |
885 | sub to_fh { |
912 | sub to_fh { |
886 | my ($self, $cb) = @_; |
913 | my ($self, $cb) = @_; |