English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NodeJS Basic Tutorial

NodeJS Express.js

NodeJS Buffer&URL;

NodeJS MySql

NodeJS MongoDB

NodeJS File (FS)

Other NodeJS

Node.js Multi-process

We all know that Node.js runs in a single-threaded mode, but it uses event-driven processing for concurrency, which helps us create multiple child processes on a multi-core CPU system to improve performance.

Each child process always has three stream objects: child.stdin, child.stdout, and child.stderr. They may share the parent process's stdio streams or be independent piped stream objects.

Node.js provides the child_process module to create child processes, with methods such as:

  • exec - child_process.exec executes commands using child processes, caches the output of child processes, and returns the output of child processes as parameters of the callback function.

  • spawn - child_process.spawn creates a new process with specified command line arguments.

  • fork}} - /son.js') is equivalent to spawn('node', ['./son.js') is equivalent to spawn('node', ['.',

exec() method

child_process.exec executes commands using child processes, caches the output of child processes, and returns the output of child processes as parameters of the callback function.

The syntax is as follows:

child_process.exec(command[, options], callback)

Parameters

Parameter Description:

command: string, the command to be executed, parameters are separated by spaces

options : object, 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 (default: in UNIX is/bin/sh, in Windows as cmd.exe, the Shell should be able to recognize -c option in UNIX, or /s /c in Windows. In Windows, command line parsing should be compatible with cmd.exe)

  • timeout, number, the timeout time (default: 0)

  • maxBuffer, number, the maximum buffer allowed in stdout or stderr (binary), if exceeded, the child process will be killed (default: 200*1024)

  • killSignal, string, the signal to end the process (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, waits for the process to end, and returns the content of the buffer all at once.

Online Examples

Let's create two js files support.js and master.js.

support.js File Code:

console.log("Process " + process.argv[2] + " Execute. ");

master.js File Code:

const fs = require('fs');
const child_process = require('child_process');
 
for(var i=0; i<3; i++) {
    var workerProcess = child_process.exec('node support.js ')+i, function (error, stdout, stderr) {
        if (error) {}}
            console.log(error.stack);
            console.log('Error code: ')+error.code);
            console.log('Signal received: ')+error.signal);
        }
        console.log('stdout: ') + stdout);
        console.log('stderr: ') + stderr);
    });
 
    workerProcess.on('exit', function(code) {
        console.log('Child process has exited, exit code ')+code);
    });
}

Execute the following code, and the output result is:

$ node master.js 
Child process has exited, exit code 0
stdout: process 1 Execution.
stderr: 
Child process has exited, exit code 0
stdout: process 0 execution.
stderr: 
Child process has exited, exit code 0
stdout: process 2 Execution.
stderr:

spawn() method

The spawn() method creates a new process with the specified command-line arguments, with the syntax format as follows:

child_process.spawn(command[, args][, options])

Parameters

Parameter Description:

command: The command to be executed

args: Array String parameter array

options Object

  • cwd String The current working directory of the child process

  • env Object The key-value pair of environment variables

  • stdio Array|String The stdio configuration of the subprocess

  • detached Boolean This subprocess will become the leader of the 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. Once the process starts executing, spawn() begins to receive responses.

Online Examples

Let's create two js files support.js and master.js.

support.js file code:

console.log("Process " + process.argv[2] + " Execute. ");

master.js file code:

const fs = require('fs');const child_process = require('child_process'); 
for(var i=0; i<3; i++) {
   var workerProcess = child_process.spawn('node', ['support.js', i]); 
   workerProcess.stdout.on('data', function(data) {
      console.log('stdout: ') + data); }); 
   workerProcess.stderr.on('data', function(data) {
      console.log('stderr: ') + data); }); 
   workerProcess.on('close', function(code) {
      console.log('Child process has exited, exit code ')+code); });}

Execute the following code, and the output result is:

$ node master.js stdout: process 0 execution.
Child process has exited, exit code 0
stdout: process 1 Execution.
Child process has exited, exit code 0
stdout: process 2 Execution.
Child process has exited, exit code 0

fork method

child_process.fork is a special form of the spawn() method, used to create processes, with the syntax format as follows:

child_process.fork(modulePath[, args][, options])

Parameters

Parameter Description:

: Array String argument arraymodulePath

: String, the module to be run in the child processargs

: Array String argument array:Object

  • cwd String The current working directory of the child process

  • env Object The key-value pair of environment variables

  • execPath String The executable file to create the child process

  • execArgv Array The string argument array for the executable file of the child process (default: process.execArgv)

  • silent Boolean If true, the stdin, stdout, and stderr of the child process will be associated with the parent process; otherwise, they will inherit from the parent process. (Default: false)

  • uid Number Sets the user process ID

  • gid Number Sets the process group ID

The returned object, in addition to having all the methods of the ChildProcess example, also has an in-built communication channel.

Online Examples

Let's create two js files support.js and master.js.

support.js File Code:

console.log("Process " + process.argv[2] + " Execute. ");

master.js File Code:

const fs = require('fs');const child_process = require('child_process'); 
for(var i=0; i<3; i++) {
   var worker_process = child_process.fork("support.js", [i]);    
 
   worker_process.on('close', function(code) {
      console.log('Child process has exited, exit code ') + code); });}

Execute the following code, and the output result is:

$ node master.js 
Process 0 Execution.
Child process has exited, exit code 0
Process 1 Execution.
Child process has exited, exit code 0
Process 2 Execution.
Child process has exited, exit code 0