Easy Tutorial
❮ Nodejs Function Nodejs Domain Module ❯

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() 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:

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

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

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.
❮ Nodejs Function Nodejs Domain Module ❯