Node.js Multiprocessing
We all know that Node.js runs in a single-threaded mode, but it uses an event-driven approach to handle concurrency. This helps us create multiple child processes on a multi-core CPU system to improve performance.
Each child process always comes with three stream objects: child.stdin, child.stdout, and child.stderr. They may share the parent process's stdio streams or can be independent redirected stream objects.
Node provides the child_process module to create child processes, with methods such as:
exec -
child_process.execexecutes a command in a child process, buffers the output, and returns it as a callback function parameter.spawn -
child_process.spawncreates a new process with the specified command line arguments.fork -
child_process.forkis a special form ofspawn()for running a module in a child process. For example,fork('./son.js')is equivalent tospawn('node', ['./son.js']). Unlike thespawnmethod,forkestablishes a communication channel between the parent and child processes for inter-process communication.
exec() Method
child_process.exec executes a command in a child process, buffers the output, and returns it as a callback function parameter.
The syntax is as follows:
child_process.exec(command[, options], callback)
Parameters
Parameter descriptions are as follows:
command: String, the command to run, with space-separated arguments.
options: Object, which can be:
cwd: String, the current working directory of the child process.env: Object, key-value pairs of environment variables.encoding: String, character encoding (default: 'utf8').shell: String, the shell to execute the command with (default:/bin/shon UNIX,cmd.exeon Windows). The shell should recognize the-cswitch on UNIX or/s /con Windows. Command line parsing should be compatible withcmd.exeon Windows.timeout: Number, the timeout duration (default: 0).maxBuffer: Number, the maximum buffer (binary) allowed in stdout or stderr. If exceeded, the child process is terminated (default: 200*1024).killSignal: String, the termination signal (default: 'SIGTERM').uid: Number, sets the user process ID.gid: Number, sets the process group ID.
callback: Callback function, containing three parameters: error, stdout, and stderr.
The exec() method returns the largest buffer and waits for the process to finish, then returns the buffered content at once.
Example
Let's create two JavaScript files, support.js and master.js.
support.js File Code:
master.js File Code:
Executing the above code will output:
$ node master.js
Child process exited with exit code 0
stdout: Process 1 executed.
stderr:
Child process exited with exit code 0
stdout: Process 0 executed.
stderr:
Child process exited with exit code 0
stdout: Process 2 executed.
stderr:
spawn() Method
child_process.spawn creates a new process with the specified command line arguments. The syntax is as follows:
child_process.spawn(command[, args][, options])
Parameters
Parameter descriptions are as follows:
command: The command to run.
args: Array of string arguments.
options: Object
cwd: String, the current working directory of the child process.env: Object, key-value pairs of environment variables.stdio: Array|String, the stdio configuration of the child process.detached: Boolean, the child process will become the leader of a process group.uid: Number, sets the user process ID.gid: Number, sets the process group ID.
The spawn() method returns streams (stdout & stderr) and is used when the process returns a large amount of data. The spawn() starts receiving responses as soon as the process starts executing.
Example
Let's create two JavaScript files, support.js and master.js.
support.js File Code:
master.js File Code:
Executing the above code will output:
$ node master.js stdout: Process 0 executed.
Child process exited with exit code 0
stdout: Process 1 executed.
Child process exited with exit code 0
Process 2 is executing.
The child process has exited with exit code 0.
fork Method
child_process.fork is a special form of the spawn() method, used for creating processes. Its syntax is as follows:
child_process.fork(modulePath[, args][, options])
Parameters
The parameters are described as follows:
modulePath: String, the module to run in the child process.
args: Array of string arguments.
options: Object
cwd: String, the current working directory of the child process.
env: Object, key-value pairs of environment variables.
execPath: String, the executable file to create the child process.
execArgv: Array, the array of string arguments for the executable file of the child process (default: process.execArgv).
silent: Boolean, if
true, the child process'sstdin,stdout, andstderrwill be piped to the parent process; otherwise, they will be inherited from the parent process. (default:false)uid: Number, sets the user ID of the process.
gid: Number, sets the group ID of the process.
The returned object has all methods of the ChildProcess instance and also includes a built-in communication channel.
Example
Let's create two JavaScript files, support.js and master.js.
support.js File Code:
master.js File Code:
Executing the above code will produce the following output:
$ node master.js
Process 0 is executing.
The child process has exited with exit code 0.
Process 1 is executing.
The child process has exited with exit code 0.
Process 2 is executing.
The child process has exited with exit code 0.