Nodejs - spawn.stdout 和 process.stdout 之间的区别
Nodejs - difference between spawn.stdout and process.stdout
玩弄 child_process,我想将生成的 cp 输出通过管道传输到自定义流。
我不明白为什么在第一种情况下管道不起作用,而在第二种情况下管道不起作用。
预设
const cp = require('child_process');
const process = require('process');
const stream = require('stream');
var writable = new stream.Writable();
writable._write = function (data) {
console.log(data.toString());
};
不起作用
var spawnedProcess = cp.spawn('ls', [], {
stdio: [process.stdin, process.stdout, process.stderr]
});
process.stdout.pipe(writable);
输出登录到终端但不通过管道传输。
有效
var spawnedProcess = cp.spawn('ls', [], {});
spawnedProcess.stdout.pipe(writable);
管道输出到 writable
。
文档说 process.stdout
is only a Writable stream so you can't pipe from it. It's weird it doesn't throw an Error: Cannot pipe. Not readable.
though. Also, cp.stdout
是一个可读流,因此它按应有的方式进行管道传输。
玩弄 child_process,我想将生成的 cp 输出通过管道传输到自定义流。
我不明白为什么在第一种情况下管道不起作用,而在第二种情况下管道不起作用。
预设
const cp = require('child_process');
const process = require('process');
const stream = require('stream');
var writable = new stream.Writable();
writable._write = function (data) {
console.log(data.toString());
};
不起作用
var spawnedProcess = cp.spawn('ls', [], {
stdio: [process.stdin, process.stdout, process.stderr]
});
process.stdout.pipe(writable);
输出登录到终端但不通过管道传输。
有效
var spawnedProcess = cp.spawn('ls', [], {});
spawnedProcess.stdout.pipe(writable);
管道输出到 writable
。
文档说 process.stdout
is only a Writable stream so you can't pipe from it. It's weird it doesn't throw an Error: Cannot pipe. Not readable.
though. Also, cp.stdout
是一个可读流,因此它按应有的方式进行管道传输。