… | |
… | |
847 | |
847 | |
848 | $self->[CB] = $cb; |
848 | $self->[CB] = $cb; |
849 | $self->_cmd (r => $func); |
849 | $self->_cmd (r => $func); |
850 | } |
850 | } |
851 | |
851 | |
|
|
852 | =back |
|
|
853 | |
|
|
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 |
|
|
921 | |
|
|
922 | These methods might go away completely or change behaviour, a any time. |
|
|
923 | |
|
|
924 | =over 4 |
|
|
925 | |
852 | =item $proc->to_fh ($cb->($fh)) # EXPERIMENTAL, MIGHT BE REMOVED |
926 | =item $proc->to_fh ($cb->($fh)) # EXPERIMENTAL, MIGHT BE REMOVED |
853 | |
927 | |
854 | Flushes all commands out to the process and then calls the callback with |
928 | Flushes all commands out to the process and then calls the callback with |
855 | the communications socket. |
929 | the communications socket. |
856 | |
930 | |