… | |
… | |
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 |
… | |
… | |
485 | my $self = shift; |
485 | my $self = shift; |
486 | |
486 | |
487 | # ideally, we would want to use "a (w/a)*" as format string, but perl |
487 | # ideally, we would want to use "a (w/a)*" as format string, but perl |
488 | # versions from at least 5.8.9 to 5.16.3 are all buggy and can't unpack |
488 | # versions from at least 5.8.9 to 5.16.3 are all buggy and can't unpack |
489 | # it. |
489 | # it. |
490 | push @{ $self->[QUEUE] }, pack "a N/a*", $_[0], $_[1]; |
490 | push @{ $self->[QUEUE] }, pack "a L/a*", $_[0], $_[1]; |
491 | |
491 | |
492 | $self->[WW] ||= AE::io $self->[FH], 1, sub { |
492 | $self->[WW] ||= AE::io $self->[FH], 1, sub { |
493 | do { |
493 | do { |
494 | # send the next "thing" in the queue - either a reference to an fh, |
494 | # send the next "thing" in the queue - either a reference to an fh, |
495 | # or a plain string. |
495 | # or a plain string. |
… | |
… | |
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 sounds |
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) |
… | |
… | |
849 | $self->_cmd (r => $func); |
859 | $self->_cmd (r => $func); |
850 | } |
860 | } |
851 | |
861 | |
852 | =back |
862 | =back |
853 | |
863 | |
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 connection. 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 | Although not a limitation of this module, keep in mind that the |
|
|
886 | "communications socket" is simply C<STDIN>, and depending on how you |
|
|
887 | started F<perl> (e.g. via F<ssh>), it might only be half-duplex. This is |
|
|
888 | fine for C<AnyEvent::Fork>, but your C<run> function might want to use |
|
|
889 | C<STDIN> (or the "communications socket") for input and C<STDOUT> for |
|
|
890 | output. |
|
|
891 | |
|
|
892 | You can support both cases by checking the C<fileno> of the handle passed |
|
|
893 | to your run function: |
|
|
894 | |
|
|
895 | sub run { |
|
|
896 | my ($rfh) = @_; |
|
|
897 | |
|
|
898 | my $wfh = fileno $rfh ? $rfh : *STDOUT; |
|
|
899 | |
|
|
900 | # now use $rfh for reading and $wfh for writing |
|
|
901 | } |
|
|
902 | |
|
|
903 | =cut |
|
|
904 | |
|
|
905 | sub new_from_stdio { |
|
|
906 | my ($class, $fh) = @_; |
|
|
907 | |
|
|
908 | my $self = $class->_new ($fh); |
|
|
909 | |
|
|
910 | # send startup code |
|
|
911 | push @{ $self->[QUEUE] }, |
|
|
912 | (do "AnyEvent/Fork/serve.pl") |
|
|
913 | . <<'EOF'; |
|
|
914 | |
|
|
915 | $OWNER = "another process"; |
|
|
916 | $0 = "AnyEvent::Fork/stdio of $OWNER"; |
|
|
917 | |
|
|
918 | serve *STDIN; |
|
|
919 | __END__ |
|
|
920 | EOF |
|
|
921 | |
|
|
922 | # the data is only sent when the user requests additional things, which |
|
|
923 | # is likely early enough for our purposes. |
|
|
924 | |
|
|
925 | $self |
|
|
926 | } |
|
|
927 | |
|
|
928 | =back |
|
|
929 | |
|
|
930 | =head2 EXPERIMENTAL METHODS |
864 | =head2 EXPERIMENTAL METHODS |
931 | |
865 | |
932 | These methods might go away completely or change behaviour, a any time. |
866 | These methods might go away completely or change behaviour, at any time. |
933 | |
867 | |
934 | =over 4 |
868 | =over 4 |
935 | |
869 | |
936 | =item $proc->to_fh ($cb->($fh)) # EXPERIMENTAL, MIGHT BE REMOVED |
870 | =item $proc->to_fh ($cb->($fh)) # EXPERIMENTAL, MIGHT BE REMOVED |
937 | |
871 | |