问题
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?
来源:https://stackoverflow.com/questions/32485674/nodejs-child-process-spawn-calling-python-script-with-sleep