Python的解释器默认启用输出缓冲sys.stdout
吗?
如果答案是肯定的,那么禁用它的所有方法是什么?
建议到目前为止:
-u
命令行开关sys.stdout
在每次写入后刷新的对象PYTHONUNBUFFERED
env varsys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
是否有任何其他方式来设置一些全局标志sys
/ sys.stdout
程序执行过程中?
我有使用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命令).