I'm trying to send a huge json string from a child process to the parent process. My initial approach was the following:
child:
process.stdout.write(myHugeJsonString);
parent:
child.stdout.on('data', function(data) { ...
But now I read that process.stdout
is blocking:
process.stderr
andprocess.stdout
are unlike other streams in Node in that writes to them are usually blocking.
- They are blocking in the case that they refer to regular files or TTY file descriptors.
- In the case they refer to pipes:
- They are blocking in Linux/Unix.
- They are non-blocking like other streams in Windows.
The documentation for child_process.spawn says I can create a pipe between the child process and the parent process using the pipe
option. But isn't piping my stdout blocking in Linux/Unix (according to cited docs above)?
Ok, what about the Stream object
option? Hmmmm, it seems I can share a readable or writable stream that refers to a socket with the child process. Would this be non-blocking? How would I implement that?
So the question stands: How do I send huge amounts of data from a child process to the parent process in a non-blocking way in Node.js? A cross-platform solution would be really neat, examples with explanation very appreciated.
stdio: 'ipc'
and sending/receiving messages that way? – Haywood