The pipe will have a finite size and if you try to cram too much stuff into it via a blocking write, then it can "fillup" and you can deadlock while trying to cram even more into the pipe. But that's not the problem in this simple example.
I think BrowserUk's suggestion is a good one. Although it is not completely clear to me why this fails. And of course, it is seldom that we have control over buffering settings the other process.
Update: Things are a lot easier if this is like a "one line in", "one line out" interface. Or if the response has a "end of transmission" flag, something like blank line means "end of transmission" so that we can quit trying to read more lines that will not be forthcoming.use strict; use warnings; use IPC::Open2; $|=1; $SIG{PIPE} = sub { print STDERR "SIGPIPE received\n"; exit; }; my $pid = open2(my $reader, my $writer, "perl echo2.pl"); print "pid=$pid\n"; print $writer "$_\n" for (qw(first second third )); print "Data written\n"; print $writer chr 4; #try manually send EOF - didn't work! close $writer; #reader still "hangs" print "Reading from pipe:\n"; while( my $in=<$reader>) { print "Received: ".$in."\n"; } close $reader; print "No more data\n"; waitpid($pid, 0); print "Finished\n"; =echo2.pl #!/usr/bin/perl -w use strict; $|++; print "first test line\n"; while (<STDIN>){print;} =cut __END__ C:\TEMP>perl ipc_open2.pl pid=5688 first second third Data written Reading from pipe: Received: first test line Received: first Received: second Received: third ####now hung up in reader ### #### for some reason it doesn't see that input pipe #### closed
use strict; use warnings; use IPC::Open2; $|=1; # this is for main program # $writer is alread unbuffered by open2 my $pid = open2(my $reader, my $writer, "perl echo2.pl"); print "pid=$pid\n"; for ( qw(first second third) ) { print $writer "$_\n"; # one line in, one line out my $result = <$reader>; print $result; } =heading #### echo2.pl ##### #!/usr/bin/perl -w use strict; $|=1; while (my $in=<STDIN>){print $in;} print STDERR "out of loop\n"; #never gets here! =cut #### echo2.pl ##### __END__ C:\TEMP>perl ipc_open2.pl pid=3244 first second third
In reply to Re: Can't get it working: Bidirectional Pipe on Windows
by Marshall
in thread Can't get it working: Bidirectional Pipe on Windows
by rovf
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |