Spawning a Process in Another Node.js Instance Using spawn()

A rather complex method of adding work to another process from a Node.js process is to spawn another process; link the stdio, stdout, and stderr pipes between them; and then execute a file on the new process, using the spawn() function. This method is a bit heavier than simply using exec() but provides some great benefits.

The major difference between spawn() and exec()/execFile() is that stdin for the spawned process can be configured, and stdout and stderr are Readable streams in the parent process. This means exec() and execFile() must complete before you can read the buffer outputs. However, you can read output data from a spawn() process as soon as it has been written.

The syntax ...

Get Node.js, MongoDB, and AngularJS Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.