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

后端 未结 3 1142
逝去的感伤
逝去的感伤 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 17:58

    Flushing the pipe doesn't happen on any fixed schedule. The only two ways that you can force the pipe to flush is by exiting the child process (which is what you're doing now), or explicitly calling flush. You can cause your handle to flush in perl by doing any of the following:

    • Adding a \n to the end of the child's message, which will (usually) cause the pipe to flush
    • Setting $| to 1, which causes the currently selected filehandle to auto-flush
    • Using IO::Handle and calling $handle->flush.
    • Using IO::Handle and setting $handle->autoflush = 1

提交回复
热议问题