… | |
… | |
450 | use AnyEvent; |
450 | use AnyEvent; |
451 | use AnyEvent::Util (); |
451 | use AnyEvent::Util (); |
452 | |
452 | |
453 | use IO::FDPass; |
453 | use IO::FDPass; |
454 | |
454 | |
455 | our $VERSION = '1.0'; |
455 | our $VERSION = 1.1; |
456 | |
456 | |
457 | # the early fork template process |
457 | # the early fork template process |
458 | our $EARLY; |
458 | our $EARLY; |
459 | |
459 | |
460 | # the empty template process |
460 | # the empty template process |
… | |
… | |
605 | |
605 | |
606 | You should use C<new> whenever possible, except when having a template |
606 | You should use C<new> whenever possible, except when having a template |
607 | process around is unacceptable. |
607 | process around is unacceptable. |
608 | |
608 | |
609 | The path to the perl interpreter is divined using various methods - first |
609 | The path to the perl interpreter is divined using various methods - first |
610 | C<$^X> is investigated to see if the path ends with something that sounds |
610 | C<$^X> is investigated to see if the path ends with something that looks |
611 | as if it were the perl interpreter. Failing this, the module falls back to |
611 | as if it were the perl interpreter. Failing this, the module falls back to |
612 | using C<$Config::Config{perlpath}>. |
612 | using C<$Config::Config{perlpath}>. |
613 | |
613 | |
|
|
614 | The path to perl can also be overriden by setting the global variable |
|
|
615 | C<$AnyEvent::Fork::PERL> - it's value will be used for all subsequent |
|
|
616 | invocations. |
|
|
617 | |
614 | =cut |
618 | =cut |
|
|
619 | |
|
|
620 | our $PERL; |
615 | |
621 | |
616 | sub new_exec { |
622 | sub new_exec { |
617 | my ($self) = @_; |
623 | my ($self) = @_; |
618 | |
624 | |
619 | return $EARLY->fork |
625 | return $EARLY->fork |
620 | if $EARLY; |
626 | if $EARLY; |
621 | |
627 | |
|
|
628 | unless (defined $PERL) { |
622 | # first find path of perl |
629 | # first find path of perl |
623 | my $perl = $; |
630 | my $perl = $; |
624 | |
631 | |
625 | # first we try $^X, but the path must be absolute (always on win32), and end in sth. |
632 | # first we try $^X, but the path must be absolute (always on win32), and end in sth. |
626 | # that looks like perl. this obviously only works for posix and win32 |
633 | # that looks like perl. this obviously only works for posix and win32 |
627 | unless ( |
634 | unless ( |
628 | ($^O eq "MSWin32" || $perl =~ m%^/%) |
635 | ($^O eq "MSWin32" || $perl =~ m%^/%) |
629 | && $perl =~ m%[/\\]perl(?:[0-9]+(\.[0-9]+)+)?(\.exe)?$%i |
636 | && $perl =~ m%[/\\]perl(?:[0-9]+(\.[0-9]+)+)?(\.exe)?$%i |
630 | ) { |
637 | ) { |
631 | # if it doesn't look perlish enough, try Config |
638 | # if it doesn't look perlish enough, try Config |
632 | require Config; |
639 | require Config; |
633 | $perl = $Config::Config{perlpath}; |
640 | $perl = $Config::Config{perlpath}; |
634 | $perl =~ s/(?:\Q$Config::Config{_exe}\E)?$/$Config::Config{_exe}/; |
641 | $perl =~ s/(?:\Q$Config::Config{_exe}\E)?$/$Config::Config{_exe}/; |
|
|
642 | } |
|
|
643 | |
|
|
644 | $PERL = $perl; |
635 | } |
645 | } |
636 | |
646 | |
637 | require Proc::FastSpawn; |
647 | require Proc::FastSpawn; |
638 | |
648 | |
639 | my ($fh, $slave) = AnyEvent::Util::portable_socketpair; |
649 | my ($fh, $slave) = AnyEvent::Util::portable_socketpair; |
… | |
… | |
647 | #local $ENV{PERL5LIB} = join ":", grep !ref, @INC; |
657 | #local $ENV{PERL5LIB} = join ":", grep !ref, @INC; |
648 | my %env = %ENV; |
658 | my %env = %ENV; |
649 | $env{PERL5LIB} = join +($^O eq "MSWin32" ? ";" : ":"), grep !ref, @INC; |
659 | $env{PERL5LIB} = join +($^O eq "MSWin32" ? ";" : ":"), grep !ref, @INC; |
650 | |
660 | |
651 | my $pid = Proc::FastSpawn::spawn ( |
661 | my $pid = Proc::FastSpawn::spawn ( |
652 | $perl, |
662 | $PERL, |
653 | ["perl", "-MAnyEvent::Fork::Serve", "-e", "AnyEvent::Fork::Serve::me", fileno $slave, $$], |
663 | ["perl", "-MAnyEvent::Fork::Serve", "-e", "AnyEvent::Fork::Serve::me", fileno $slave, $$], |
654 | [map "$_=$env{$_}", keys %env], |
664 | [map "$_=$env{$_}", keys %env], |
655 | ) or die "unable to spawn AnyEvent::Fork server: $!"; |
665 | ) or die "unable to spawn AnyEvent::Fork server: $!"; |
656 | |
666 | |
657 | $self->_new ($fh, $pid) |
667 | $self->_new ($fh, $pid) |
658 | } |
668 | } |
659 | |
669 | |
660 | =item $pid = $proc->pid |
670 | =item $pid = $proc->pid |
661 | |
671 | |
662 | 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 |
663 | 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. |
664 | |
677 | |
665 | 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 |
666 | 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, |
667 | to clean up their zombies when they die. |
680 | you need to check whether a process is a diretc child by calling this |
668 | |
681 | method, and possibly creating a child watcher or reap it manually. |
669 | All other processes are not direct children, and will be cleaned up by |
|
|
670 | AnyEvent::Fork itself. |
|
|
671 | |
682 | |
672 | =cut |
683 | =cut |
673 | |
684 | |
674 | sub pid { |
685 | sub pid { |
675 | $_[0][PID] |
686 | $_[0][PID] |
… | |
… | |
806 | |
817 | |
807 | 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 |
808 | 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 |
809 | 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 |
810 | 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 |
811 | |
848 | |
812 | 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 |
813 | 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. |
814 | |
851 | |
815 | my $pool = AnyEvent::Fork |
852 | my $pool = AnyEvent::Fork |
… | |
… | |
849 | $self->_cmd (r => $func); |
886 | $self->_cmd (r => $func); |
850 | } |
887 | } |
851 | |
888 | |
852 | =back |
889 | =back |
853 | |
890 | |
854 | =head2 ADVANCED METHODS |
|
|
855 | |
|
|
856 | =over 4 |
|
|
857 | |
|
|
858 | =item new_from_stdio AnyEvent::Fork $fh |
|
|
859 | |
|
|
860 | Assume that you have a perl interpreter running (without any special |
|
|
861 | options or a program) somewhere and it has it's STDIN and STDOUT connected |
|
|
862 | to the C<$fh> somehow. I.e. exactly the state perl is in when you start it |
|
|
863 | without any arguments: |
|
|
864 | |
|
|
865 | perl |
|
|
866 | |
|
|
867 | Then you can create an C<AnyEvent::Fork> object out of this perl |
|
|
868 | interpreter with this constructor. |
|
|
869 | |
|
|
870 | When the usefulness of this isn't immediately clear, imagine you manage to |
|
|
871 | run a perl interpreter remotely (F<ssh remotemachine perl>), then you can |
|
|
872 | manage it mostly like a local C<AnyEvent::Fork> child. |
|
|
873 | |
|
|
874 | This works without any module support, i.e. the remote F<perl> does not |
|
|
875 | need to have any special modules installed. |
|
|
876 | |
|
|
877 | There are a number of limitations though: C<send_fh> will only work if the |
|
|
878 | L<IO::FDPass> module is loadable by the remote perl and the two processes |
|
|
879 | are connected in a way that let's L<IO::FDPass> do it's work. |
|
|
880 | |
|
|
881 | This will therefore not work over a network conenction. From this follows |
|
|
882 | that C<fork> will also not work under these circumstances, as it relies on |
|
|
883 | C<send_fh> internally. |
|
|
884 | |
|
|
885 | =cut |
|
|
886 | |
|
|
887 | sub new_from_stdio { |
|
|
888 | my ($class, $fh) = @_; |
|
|
889 | |
|
|
890 | my $self = $class->_new ($fh); |
|
|
891 | |
|
|
892 | # send startup code |
|
|
893 | push @{ $self->[QUEUE] }, |
|
|
894 | (do "AnyEvent/Fork/serve.pl") |
|
|
895 | . <<'EOF'; |
|
|
896 | { |
|
|
897 | open my $fh, "+<&0" |
|
|
898 | or die "AnyEvent::Fork::Serve::stdio: unable to open communications socket: $!\n"; |
|
|
899 | open STDIN , ">&2"; |
|
|
900 | open STDOUT, ">&2"; |
|
|
901 | |
|
|
902 | $OWNER = "another process"; |
|
|
903 | $0 = "AnyEvent::Fork/stdio of $OWNER"; |
|
|
904 | |
|
|
905 | @_ = $fh; |
|
|
906 | } |
|
|
907 | |
|
|
908 | &serve; |
|
|
909 | __END__ |
|
|
910 | EOF |
|
|
911 | |
|
|
912 | # the data is only sent when the user requests additional things, which |
|
|
913 | # is likely early enough for our purposes. |
|
|
914 | |
|
|
915 | $self |
|
|
916 | } |
|
|
917 | |
|
|
918 | =back |
|
|
919 | |
|
|
920 | =head2 EXPERIMENTAL METHODS |
891 | =head2 EXPERIMENTAL METHODS |
921 | |
892 | |
922 | These methods might go away completely or change behaviour, a any time. |
893 | These methods might go away completely or change behaviour, at any time. |
923 | |
894 | |
924 | =over 4 |
895 | =over 4 |
925 | |
896 | |
926 | =item $proc->to_fh ($cb->($fh)) # EXPERIMENTAL, MIGHT BE REMOVED |
897 | =item $proc->to_fh ($cb->($fh)) # EXPERIMENTAL, MIGHT BE REMOVED |
927 | |
898 | |
… | |
… | |
929 | the communications socket. |
900 | the communications socket. |
930 | |
901 | |
931 | The process object becomes unusable on return from this function - any |
902 | The process object becomes unusable on return from this function - any |
932 | further method calls result in undefined behaviour. |
903 | further method calls result in undefined behaviour. |
933 | |
904 | |
934 | 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 |
935 | 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 |
936 | 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, |
937 | effectively passing a fork object to another process. |
908 | thereby effectively passing a fork object to another process. |
938 | |
909 | |
939 | =cut |
910 | =cut |
940 | |
911 | |
941 | sub to_fh { |
912 | sub to_fh { |
942 | my ($self, $cb) = @_; |
913 | my ($self, $cb) = @_; |
… | |
… | |
1117 | (part of this distribution). |
1088 | (part of this distribution). |
1118 | |
1089 | |
1119 | L<AnyEvent::Fork::Template>, to create a process by forking the main |
1090 | L<AnyEvent::Fork::Template>, to create a process by forking the main |
1120 | program at a convenient time (part of this distribution). |
1091 | program at a convenient time (part of this distribution). |
1121 | |
1092 | |
|
|
1093 | L<AnyEvent::Fork::Remote>, for another way to create processes that is |
|
|
1094 | mostly compatible to this module and modules building on top of it, but |
|
|
1095 | works better with remote processes. |
|
|
1096 | |
1122 | L<AnyEvent::Fork::RPC>, for simple RPC to child processes (on CPAN). |
1097 | L<AnyEvent::Fork::RPC>, for simple RPC to child processes (on CPAN). |
1123 | |
1098 | |
1124 | L<AnyEvent::Fork::Pool>, for simple worker process pool (on CPAN). |
1099 | L<AnyEvent::Fork::Pool>, for simple worker process pool (on CPAN). |
1125 | |
1100 | |
1126 | =head1 AUTHOR AND CONTACT INFORMATION |
1101 | =head1 AUTHOR AND CONTACT INFORMATION |