Additional stdio threads for node.js process

Node.js API documents using optional stdio (fd = 4) at the birth of the child process:

// Open an extra fd=4, to interact with programs present a // startd-style interface. spawn('prg', [], { stdio: ['pipe', null, null, null, 'pipe'] }); 

This stdio will be available to the parent process through ChildProcess.stdio[fd] .

How can a child process access these additional stdios? Let instead of a stream, use the stream in file descriptor 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] }); 
+6
source share
1 answer

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

I did not find anything from checking the process object, which indicates that these stdios are available for the child process, as far as I know, you can not determine whether these stdios are available from the child.

However, if you know for sure that your child process will be created using these stdios, you can use the read / write functions as follows:

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

All Articles