title: Getting Python output in real-time from C++/boost::process
body: <p>From a C++ program (running under Windows 10), I use boost::process to invoke Python in order to interpret a simple Python script. I want to redirect Python script's output in real-time to my C++ program's console.</p>
  <p>My problem is that I'm getting the whole Python script output at once when the program completed, I'm not getting it in real-time.</p>
  <p>Here is my MCVE:</p>
  <p><strong>Python script (script.py):</strong></p>
  <pre><code>import time
  from datetime import datetime
  
  print( &quot;Started Python script at t=&quot; + str(datetime.now().time()) )
  
  time.sleep(1)
  print( &quot;Slept 1 sec, t=&quot; + str(datetime.now().time()) )
  time.sleep(1)
  print( &quot;Slept 1 sec, t=&quot; + str(datetime.now().time()) )
  
  print( &quot;Stopped Python script at t=&quot; + str(datetime.now().time()) )
  </code></pre>
  <p><strong>C++ program (main.cpp):</strong></p>
  <pre><code>#include &lt;boost/process.hpp&gt;
  #include &lt;ctime&gt;
  #include &lt;iostream&gt;
  #include &lt;chrono&gt;
  
  namespace bp = boost::process;
  
  std::ostream &amp;operator&lt;&lt;(std::ostream &amp;stream, const std::chrono::system_clock::time_point&amp; time_point) 
  {
      const auto time {std::chrono::system_clock::to_time_t (time_point)};
      const auto localtime {*std::localtime (&amp;time)};
      const auto time_since_epoch {time_point.time_since_epoch()};
      const auto milliseconds_count {std::chrono::duration_cast&lt;std::chrono::milliseconds&gt; (time_since_epoch).count() % 1000};
      
      stream &lt;&lt; &quot;[&quot; &lt;&lt; std::put_time (&amp;localtime, &quot;%T&quot;) &lt;&lt; &quot;.&quot; &lt;&lt; std::setw (3) &lt;&lt; std::setfill ('0') &lt;&lt; milliseconds_count &lt;&lt; &quot;] - &quot;;
      return stream;
  }
  
  int main( int argc, char* argv[] )
  {
      std::cout &lt;&lt; std::chrono::system_clock::now() &lt;&lt; &quot;Creating child&quot; &lt;&lt; std::endl;
      bp::ipstream stream;
      bp::child c(&quot;python.exe&quot;, &quot;script.py&quot;, bp::std_out &gt; stream);
      std::cout &lt;&lt; std::chrono::system_clock::now() &lt;&lt; &quot;Created child&quot; &lt;&lt; std::endl;
  
      std::cout &lt;&lt; std::chrono::system_clock::now() &lt;&lt; &quot;Invoking getline&quot; &lt;&lt; std::endl;
      std::string line;
      while (getline(stream, line)) {
          std::cout &lt;&lt; std::chrono::system_clock::now() &lt;&lt; &quot;From Python output: &quot; &lt;&lt; line &lt;&lt; std::endl;
      }
      std::cout &lt;&lt; std::chrono::system_clock::now() &lt;&lt; &quot;getline ended&quot; &lt;&lt; std::endl;
  
      c.wait();
  
      return 0;
  }
  </code></pre>
  <p>This program outputs:</p>
  <blockquote>
  <p>[12:50:34.684] - Creating child<br/>
  [12:50:34.706] - Created child<br/>
  [12:50:34.708] - Invoking getline<br/>
  [12:50:36.743] - From Python output: Started Python script at t=12:50:34.742105<br/>
  [12:50:36.745] - From Python output: Slept 1 sec, t=12:50:35.743111<br/>
  [12:50:36.745] - From Python output: Slept 1 sec, t=12:50:36.743328<br/>
  [12:50:36.746] - From Python output: Stopped Python script at t=12:50:36.743328<br/>
  [12:50:36.747] - getline ended<br/></p>
  </blockquote>
  <p>As you can see, we get the whole 4 outputs after Python process ended (first call to <code>getline</code> freezes for 2 seconds - while Python runs the two <code>time.sleep(1)</code> - and then within 3ms we get the whole 4 lines). I would expect to get something like:</p>
  <blockquote>
  <p>[12:50:34.684] - Creating child<br/>
  [12:50:34.706] - Created child<br/>
  [12:50:34.708] - Invoking getline<br/>
  [12:50:3<strong>4.XXX</strong>] - From Python output: Started Python script at t=12:50:34.742105<br/>
  [12:50:3<strong>5.XXX</strong>] - From Python output: Slept 1 sec, t=12:50:35.743111<br/>
  [12:50:36.XXX] - From Python output: Slept 1 sec, t=12:50:36.743328<br/>
  [12:50:36.XXX] - From Python output: Stopped Python script at t=12:50:36.743328<br/>
  [12:50:36.XXX] - getline ended<br/></p>
  </blockquote>
  <p>I suspect the problem comes more from <code>boost::process</code> than from Python, but all the examples I could find for <code>boost::process</code> are reading <code>std::cout</code> the same way. Is there any thing I should change to have the <code>getline</code> loop run in real-time while Python prints output?</p>