1

I'm testing on nodejs child_process module to read the stdout from my python script. However, I noticed when my python emit message between every second. The nodejs callback can only collect the stdout at the end of the python script ends.

Python Script

 import time
 for i in range(0,5):
   ····print i
   ····time.sleep(0.5)

and the nodejs script is

var cp = require('child_process');
var spw = cp.spawn('python', ['tql.py']),
    str = "";
spw.stdout.on('data', function (data) {
    str+= data;
    console.log(data);
});

spw.on('close', function (code) {
    console.log(str);
});

spw.stderr.on('data', function (data) {
    console.log('stderr: ' + data);
});

Instead of emit message each second, my program only calls the callback when the python script ends. Is there anything I need to know in order to achieve my goal?

Jian
  • 43
  • 6
  • What output do you get? Why are you logging "data" instead of data (without the quotes) on console.log inside spw.stdout.on? – lsampaio Sep 09 '15 at 19:33
  • Oops, it was a typo. I want the result can be output as the way python does. The current program can only "dump" the output when the python script ends instead of printing the result with each second during the script running. – Jian Sep 09 '15 at 22:45
  • Got it, could you check this similar thread to see if it works? http://stackoverflow.com/questions/16873116/sending-serial-data-from-python-script-to-node-script – lsampaio Sep 11 '15 at 15:15

0 Answers0