node.js进程的额外stdio流

时间:2022-09-06 16:36:42

The node.js API documents using an extra stdio (fd=4) when spawning a child process:

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'] });

That stdio would be available to the parent process via ChildProcess.stdio[fd].

通过ChildProcess.stdio [fd],父进程可以使用该stdio。

How can the child process access these extra stdios? Let's use a stream instead of a pipe on file descriptor 3 (fd=3).

子进程如何访问这些额外的stdios?让我们在文件描述符3(fd = 3)上使用流而不是管道。

/* 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] });

1 个解决方案

#1


7  

Although node.js does not document this in the API, you can read/write to these streams with the index number of the file descriptor using fs.read and fs.write.

尽管node.js没有在API中记录这一点,但您可以使用fs.read和fs.write使用文件描述符的索引号读取/写入这些流。

I have not found anything from inspecting the process object that indicates the presence of these stdios available to the child process, so as far as I know, you would not be able to detect whether or not those stdios are available from the child.

我没有找到任何检查过程对象的信息,这些过程对象表明这些stdios可用于子进程,所以据我所知,你将无法检测到这些stdios是否可以从子进程获得。

However, if you know for sure that your child process will be spawned with these stdios, then you can use read/write functions like so:

但是,如果您确定您的子进程将使用这些stdios生成,那么您可以使用读/写函数,如下所示:

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
});

#1


7  

Although node.js does not document this in the API, you can read/write to these streams with the index number of the file descriptor using fs.read and fs.write.

尽管node.js没有在API中记录这一点,但您可以使用fs.read和fs.write使用文件描述符的索引号读取/写入这些流。

I have not found anything from inspecting the process object that indicates the presence of these stdios available to the child process, so as far as I know, you would not be able to detect whether or not those stdios are available from the child.

我没有找到任何检查过程对象的信息,这些过程对象表明这些stdios可用于子进程,所以据我所知,你将无法检测到这些stdios是否可以从子进程获得。

However, if you know for sure that your child process will be spawned with these stdios, then you can use read/write functions like so:

但是,如果您确定您的子进程将使用这些stdios生成,那么您可以使用读/写函数,如下所示:

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
});