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?

3
  • What output do you get? Why are you logging "data" instead of data (without the quotes) on console.log inside spw.stdout.on? Commented Sep 9, 2015 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. Commented Sep 9, 2015 at 22:45
  • Got it, could you check this similar thread to see if it works? stackoverflow.com/questions/16873116/… Commented Sep 11, 2015 at 15:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.