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.52 by root, Thu Apr 25 11:40:42 2013 UTC vs.
Revision 1.54 by root, Fri Apr 26 17:24:05 2013 UTC

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 L/a*", $_[0], $_[1]; 490 push @{ $self->[QUEUE] }, pack "a N/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.
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
860Assume that you have a perl interpreter running (without any special
861options or a program) somewhere and it has it's STDIN and STDOUT connected
862to the C<$fh> somehow. I.e. exactly the state perl is in when you start it
863without any arguments:
864
865 perl
866
867Then you can create an C<AnyEvent::Fork> object out of this perl
868interpreter with this constructor.
869
870When the usefulness of this isn't immediately clear, imagine you manage to
871run a perl interpreter remotely (F<ssh remotemachine perl>), then you can
872manage it mostly like a local C<AnyEvent::Fork> child.
873
874This works without any module support, i.e. the remote F<perl> does not
875need to have any special modules installed.
876
877There are a number of limitations though: C<send_fh> will only work if the
878L<IO::FDPass> module is loadable by the remote perl and the two processes
879are connected in a way that let's L<IO::FDPass> do it's work.
880
881This will therefore not work over a network connection. From this follows
882that C<fork> will also not work under these circumstances, as it relies on
883C<send_fh> internally.
884
885Although not a limitation of this module, keep in mind that the
886"communications socket" is simply C<STDIN>, and depending on how you
887started F<perl> (e.g. via F<ssh>), it might only be half-duplex. This is
888fine for C<AnyEvent::Fork>, but your C<run> function might want to use
889C<STDIN> (or the "communications socket") for input and C<STDOUT> for
890output.
891
892You can support both cases by checking the C<fileno> of the handle passed
893to 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
905sub 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
918serve *STDIN;
919__END__
920EOF
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
931
932These methods might go away completely or change behaviour, a any time.
933
934=over 4
935
852=item $proc->to_fh ($cb->($fh)) # EXPERIMENTAL, MIGHT BE REMOVED 936=item $proc->to_fh ($cb->($fh)) # EXPERIMENTAL, MIGHT BE REMOVED
853 937
854Flushes all commands out to the process and then calls the callback with 938Flushes all commands out to the process and then calls the callback with
855the communications socket. 939the communications socket.
856 940

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines