小编iFa*_*dey的帖子

以流动模式流式传输子进程输出

我有使用Python编写的自定义命令行,它使用"print"语句打印其输出.我通过生成子进程并使用child.stdin.write方法向它发送命令从Node.js中使用它.这是来源:

var childProcess = require('child_process'),
    spawn = childProcess.spawn;

var child = spawn('./custom_cli', ['argument_1', 'argument_2']);

child.stdout.on('data', function (d) {
  console.log('out: ' + d);
});

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

//execute first command after 1sec
setTimeout(function () {
  child.stdin.write('some_command' + '\n');
}, 1000);

//execute "quit" command after 2sec
//to terminate the command line
setTimeout(function () {
  child.stdin.write('quit' + '\n');
}, 2000);
Run Code Online (Sandbox Code Playgroud)

现在问题是我没有在流动模式下接收输出.我希望在打印后立即从子进程获取输出,但只有在子进程终止时才会收到所有命令的输出(使用自定义cli的quit命令).

javascript python node.js

10
推荐指数
2
解决办法
3377
查看次数

创建全局“for”变量。应该是 'for (var items ...'

我正在使用 jshint。谁能告诉我为什么它将“for”关键字视为全局变量?

创建全局“for”变量。应该是 'for (var items ...'

这是循环:

//items and properties are defined above...
var items = null, properties = someObject;

//code here is properly terminated with ; "semicolon"

for (items in properties) {
    if (properties.hasOwnProperty(items)) {
        //some code here...
    }
}
Run Code Online (Sandbox Code Playgroud)

javascript jslint jshint

4
推荐指数
2
解决办法
2488
查看次数

标签 统计

javascript ×2

jshint ×1

jslint ×1

node.js ×1

python ×1