title: Node.js's python child script outputting on finish, not real time
body: <p>I am new to node.js and socket.io and I am trying to write a small server that will update a webpage based on python output. </p>
  
  <p>Eventually this will be used for a temperature sensor so for now I have a dummy script which prints temperature values every few seconds:</p>
  
  <p><strong>Thermostat.py</strong></p>
  
  <pre><code>import random, time
  for x in range(10):
      print(str(random.randint(23,28))+" C")
      time.sleep(random.uniform(0.4,5))
  </code></pre>
  
  <p>Here's a cut down version of the server:</p>
  
  <p><strong>Index.js</strong></p>
  
  <pre><code>var sys   = require('sys'), 
      spawn = require('child_process').spawn, 
      thermostat = spawn('python', ["thermostat.py"]),
      app = require('express')(),
      http = require('http').Server(app),
      io = require('socket.io')(http);
  
  thermostat.stdout.on('data', function (output) { 
      var temp = String(output);
      console.log(temp);
      io.sockets.emit('temp-update', { data: temp});
  }); 
  
  app.get('/', function(req, res){
      res.sendFile(__dirname + '/index.html');
      });
  </code></pre>
  
  <p>And finally the web page:</p>
  
  <p><strong>Index.html</strong></p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Live temperature&lt;/title&gt;
          &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;
      &lt;/head&gt;
      &lt;body&gt;
      &lt;div id="liveTemp"&gt;Loading...&lt;/div&gt;
  
      &lt;script src="http://code.jquery.com/jquery-1.11.1.js"&gt;&lt;/script&gt;
      &lt;script src="/socket.io/socket.io.js"&gt;&lt;/script&gt;
      &lt;script&gt;
          var socket = io();
          socket.on('temp-update', function (msg) {
          $('#liveTemp').html(msg.data)
      });
      &lt;/script&gt;
  
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The problem is nodejs seems to recieve all of the temperature values at once, and instead of getting 10 temperature values at random intervals, I get <em>all</em> of the values in one long string after the script has finished:</p>
  
  <p><img src="https://i.stack.imgur.com/f3qP9.png" alt="lots of temp values">
  <img src="https://i.stack.imgur.com/0Nzn0.png" alt="console output"></p>