in reply to IPC::Open3 & closed STDOUT
You are making the assumption (maybe you don't realize that you are) that your subprocess is working in a very 1, 2, 3 fashion. That is, it reads STDIN (completely), and then it writes STDOUT (completely) and then it is done (never writing STDERR... except of course, unless it explicitly closes its STDOUT before doing its writes to STDERR, which I'm sure it doesn't).chomp(@outlines = <CHILD_OUT>); close (CHILD_OUT); chomp(@errlines = <CHILD_ERR>); close (CHILD_ERR);
If it writes anything to STDERR, then everything will hang. The reason is that the two processes are deadlocked. The first is waiting on the second to write STDOUT, and the second is waiting on the first to read STDERR. One thing you may have missed in the IPC::open3 documentation is the fact that these pipes are synchronized between the parent and child. If the child is trying to write STDERR, then the parent must be reading the child's STDERR. If not, then the child will wait until the parent is ready to read. Likewise, if the parent is reading the child's STDOUT, then it will wait until the child writes STDOUT.
So how do you deal with this? You either do it the hard way, which is to learn about IO::Select and non-blocking I/O. (That is, instead of having the parent say "read the child's STDOUT", say "try to read the child's STDOUT, if the child is trying to write STDOUT, else try to read the child's STDERR.... etc"). Or, you do it the easy, somewhat hackish way, which is to either ignore STDERR or lump it together with STDOUT. You can look into IPC::open2 for that tack. Of course, this all, also, relies on the assumption that the subprocess will do all of its reading before it does any of its writing.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: IPC::Open3 & closed STDOUT (++ +2)
by tye (Sage) on Nov 19, 2003 at 15:51 UTC |