Nodejs child_process spawn calling python script with sleep

烂漫一生 提交于 2019-12-12 00:26:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!