用于node.js进程的额外stdiostream
当生成subprocess时,node.js API文档使用额外的stdio(fd = 4):
// Open an extra fd=4, to interact with programs present a // startd-style interface. spawn('prg', [], { stdio: ['pipe', null, null, null, 'pipe'] });
该stdio通过ChildProcess.stdio[fd]
可用于父进程。
孩子如何能够访问这些额外的工作室? 让我们在文件描述符3(fd = 3)上使用stream而不是pipe道。
/* parent process */ // open file for read/write var mStream = fs.openSync('./shared-stream', 'r+'); // spawn child process with stream object as fd=3 spawn('node', ['/path/to/child.js'], {stdio: [0, 1, 2, mStream] });
尽pipenode.js没有在API中logging这些信息,但是您可以使用fs.read
和fs.write
读取/写入这些具有文件描述符索引号的stream。
我没有发现任何东西来检查process
对象,表明这些工作室可用于subprocess,所以据我所知,你将无法检测到这些工作室是否可以从孩子。
但是,如果您确实知道您的subprocess将由这些工作室产生,那么您可以使用如下所示的读/写function:
var fd_index = 3; fs.write(fd_index, new Buffer(data, 'utf8'), 0, data.length, null, function(err, bytesWritten, buffer) { if(err) return failure(); else ... // success });