Why doesn't my parent process see the child's output until it exits?

后端 未结 3 1141
逝去的感伤
逝去的感伤 2020-12-11 17:50

Consider the following script:

use IO::File;
$| = 1;
my ($handle, $pid) = myPipe();
if ($pid == 0) {
  print \"$$\";
  sleep 5;
  exit;
}

print \"child: \".         


        
3条回答
  •  情歌与酒
    2020-12-11 18:17

    On some (most?) systems, pipes use I/O buffering by default. Put a

    $handle->autoflush(1);
    

    statement in your myPipe function.

    But even when buffering is turned off, Perl still doesn't flush except after a newline. So you may also want your child process to include a newline in the output.


    Update: Testing your code (Cygwin, perl 5.10.0, YMMV), I see the issue is a lack of the newline in the child output, not whether autoflush is explicitly turned on when the pipe is created.

提交回复
热议问题