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

Comparing AnyEvent-Fork/Fork.pm (file contents):
Revision 1.56 by root, Sun Apr 28 13:47:52 2013 UTC vs.
Revision 1.60 by root, Wed Sep 25 11:05:30 2013 UTC

450use AnyEvent; 450use AnyEvent;
451use AnyEvent::Util (); 451use AnyEvent::Util ();
452 452
453use IO::FDPass; 453use IO::FDPass;
454 454
455our $VERSION = 1.1; 455our $VERSION = 1.2;
456 456
457# the early fork template process 457# the early fork template process
458our $EARLY; 458our $EARLY;
459 459
460# the empty template process 460# the empty template process
605 605
606You should use C<new> whenever possible, except when having a template 606You should use C<new> whenever possible, except when having a template
607process around is unacceptable. 607process around is unacceptable.
608 608
609The path to the perl interpreter is divined using various methods - first 609The path to the perl interpreter is divined using various methods - first
610C<$^X> is investigated to see if the path ends with something that sounds 610C<$^X> is investigated to see if the path ends with something that looks
611as if it were the perl interpreter. Failing this, the module falls back to 611as if it were the perl interpreter. Failing this, the module falls back to
612using C<$Config::Config{perlpath}>. 612using C<$Config::Config{perlpath}>.
613 613
614The path to perl can also be overriden by setting the global variable 614The path to perl can also be overriden by setting the global variable
615C<$AnyEvent::Fork::PERL> - it's value will be used for all subsequent 615C<$AnyEvent::Fork::PERL> - it's value will be used for all subsequent
668} 668}
669 669
670=item $pid = $proc->pid 670=item $pid = $proc->pid
671 671
672Returns the process id of the process I<iff it is a direct child of the 672Returns the process id of the process I<iff it is a direct child of the
673process running AnyEvent::Fork>, and C<undef> otherwise. 673process running AnyEvent::Fork>, and C<undef> otherwise. As a general
674rule (that you cannot rely upon), processes created via C<new_exec>,
675L<AnyEvent::Fork::Early> or L<AnyEvent::Fork::Template> are direct
676children, while all other processes are not.
674 677
675Normally, only processes created via C<< AnyEvent::Fork->new_exec >> and 678Or in other words, you do not normally have to take care of zombies for
676L<AnyEvent::Fork::Template> are direct children, and you are responsible 679processes created via C<new>, but when in doubt, or zombies are a problem,
677to clean up their zombies when they die. 680you need to check whether a process is a diretc child by calling this
678 681method, and possibly creating a child watcher or reap it manually.
679All other processes are not direct children, and will be cleaned up by
680AnyEvent::Fork itself.
681 682
682=cut 683=cut
683 684
684sub pid { 685sub pid {
685 $_[0][PID] 686 $_[0][PID]
816 817
817Even if not used otherwise, the socket can be a good indicator for the 818Even if not used otherwise, the socket can be a good indicator for the
818existence of the process - if the other process exits, you get a readable 819existence of the process - if the other process exits, you get a readable
819event on it, because exiting the process closes the socket (if it didn't 820event on it, because exiting the process closes the socket (if it didn't
820create any children using fork). 821create any children using fork).
822
823=over 4
824
825=item Compatibility to L<AnyEvent::Fork::Remote>
826
827If you want to write code that works with both this module and
828L<AnyEvent::Fork::Remote>, you need to write your code so that it assumes
829there are two file handles for communications, which might not be unix
830domain 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
839This checks whether the passed file handle is, in fact, the process
840C<STDIN> handle. If it is, then the function was invoked visa
841L<AnyEvent::Fork::Remote>, so STDIN should be used for reading and
842C<STDOUT> should be used for writing.
843
844In all other cases, the function was called via this module, and there is
845only one file handle that should be sued for reading and writing.
846
847=back
821 848
822Example: create a template for a process pool, pass a few strings, some 849Example: create a template for a process pool, pass a few strings, some
823file handles, then fork, pass one more string, and run some code. 850file handles, then fork, pass one more string, and run some code.
824 851
825 my $pool = AnyEvent::Fork 852 my $pool = AnyEvent::Fork
873the communications socket. 900the communications socket.
874 901
875The process object becomes unusable on return from this function - any 902The process object becomes unusable on return from this function - any
876further method calls result in undefined behaviour. 903further method calls result in undefined behaviour.
877 904
878The point of this method is to give you a file handle thta you cna pass 905The point of this method is to give you a file handle that you can pass
879to another process. In that other process, you can call C<new_from_fh 906to another process. In that other process, you can call C<new_from_fh
880AnyEvent::Fork> to create a new C<AnyEvent::Fork> object from it, thereby 907AnyEvent::Fork $fh> to create a new C<AnyEvent::Fork> object from it,
881effectively passing a fork object to another process. 908thereby effectively passing a fork object to another process.
882 909
883=cut 910=cut
884 911
885sub to_fh { 912sub to_fh {
886 my ($self, $cb) = @_; 913 my ($self, $cb) = @_;
1061(part of this distribution). 1088(part of this distribution).
1062 1089
1063L<AnyEvent::Fork::Template>, to create a process by forking the main 1090L<AnyEvent::Fork::Template>, to create a process by forking the main
1064program at a convenient time (part of this distribution). 1091program at a convenient time (part of this distribution).
1065 1092
1093L<AnyEvent::Fork::Remote>, for another way to create processes that is
1094mostly compatible to this module and modules building on top of it, but
1095works better with remote processes.
1096
1066L<AnyEvent::Fork::RPC>, for simple RPC to child processes (on CPAN). 1097L<AnyEvent::Fork::RPC>, for simple RPC to child processes (on CPAN).
1067 1098
1068L<AnyEvent::Fork::Pool>, for simple worker process pool (on CPAN). 1099L<AnyEvent::Fork::Pool>, for simple worker process pool (on CPAN).
1069 1100
1070=head1 AUTHOR AND CONTACT INFORMATION 1101=head1 AUTHOR AND CONTACT INFORMATION

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines