Node.js運行在一個單線程模式,但它使用一個事件驅動范例來處理并發(fā)。它還有助于創(chuàng)建子進程,以充分利用并行處理的多核CPU系統(tǒng)。
子進程總是有三個流child.stdin,child.stdout和child.stderr這可能與父進程stdio流共享。
Node提供child_process模塊,該模塊具有以下三個主要的方法來創(chuàng)建子進程。
exec - child_process.exec方法在shell/控制臺運行一個命令并緩沖輸出。
spawn - child_process.spawn啟動一個新的過程,一個給定的指令
fork - child_process.fork方法是指定spawn()來創(chuàng)建子進程的一個特例。
child_process.exec方法在shell運行一個命令并緩沖輸出。它具有以下特征:
child_process.exec(command[, options], callback)
下面是使用的參數(shù)的說明:
command 字符串是要運行的命令,用空格分隔的參數(shù)
options 對象可包括以下一個或多個選項:
cwd 子進程的字符串當前工作目錄
env 對象環(huán)境的鍵值對
encoding 字符串(缺?。?ldquo;UTF8”)
shell 字符串執(zhí)行(默認命令:在UNIX上為 '/bin/sh',在Windows為cmd.exe“, 在shell應該明白/s /c 在Windows或 -c 在UNIX/;在Windows中,命令行解析應與cmd.exe兼容)
timeout 數(shù)字(默認: 0)
maxBuffer 數(shù)字(默認: 200*1024)
killSignal 字符串 (默認: 'SIGTERM')
uid 數(shù)字用于設置過程的用戶的身份
gid 數(shù)字設置進程的組標識
callback 函數(shù)獲取三個參數(shù)錯誤,輸出和錯誤被稱為與下面的輸出,當進程終止。
在exec()方法返回一個緩沖帶最大尺寸,等待結束該進程,并嘗試一次返回所有緩存數(shù)據(jù)。
讓我們創(chuàng)建兩個JS文件名分別為:support.js和master.js:
File: support.js
console.log("Child Process " + process.argv[2] + " executed." );
File: master.js
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 exited with exit code '+code);
});
}
現(xiàn)在運行master.js看到結果:
$ node master.js
驗證輸出。服務器已經(jīng)啟動
Child process exited with exit code 0 stdout: Child Process 1 executed. stderr: Child process exited with exit code 0 stdout: Child Process 0 executed. stderr: Child process exited with exit code 0 stdout: Child Process 2 executed.
child_process.spawn 方法啟動使用給定命令一個新的進程。它具有以下特征:
child_process.spawn(command[, args][, options])
下面是使用的參數(shù)的說明:
command 字符串運行的命令
args 字符串參數(shù)數(shù)組列表
options 對象可包括以下一個或多個選項:
cwd 子進程的字符串為當前工作目錄
env 對象環(huán)境的鍵值對
stdio 數(shù)組|子串的標準輸入輸出配置
customFds 數(shù)組,不推薦使用文件描述符的子數(shù)組,使用作為標準輸入輸出
detached 布爾將是一個子進程組
uid 數(shù)目設置進程的用戶的身份。
gid 數(shù)目設置進程的組標識。
spawn()方法返回流(標準輸出與標準錯誤),它當處理返回大量的數(shù)據(jù)被使用。spawn()開始接收到響應的進程開始執(zhí)行。
創(chuàng)建兩個JS文件名分別為support.js和master.js:
File: support.js
console.log("Child Process " + process.argv[2] + " executed." );
File: master.js
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 exited with code ' + code);
});
}
現(xiàn)在運行master.js看到的結果:
$ node master.js
驗證輸出。服務器已經(jīng)啟動
stdout: Child Process 0 executed. child process exited with code 0 stdout: Child Process 1 executed. stdout: Child Process 2 executed. child process exited with code 0 child process exited with code 0
child_process.fork方法是spawn()來創(chuàng)建節(jié)點的過程的一個特例。它具有以下簽名
child_process.fork(modulePath[, args][, options])
下面是使用的參數(shù)的說明:
modulePath 字符串 - 該模塊在運行子進程
args 字符串 - 參數(shù)數(shù)組列表
options 對象可包括以下一個或多個選項:
cwd 字符串 - 子進程的當前工作目錄
env 對象環(huán)境的鍵值對
execPath 字符串 - 可執(zhí)行用于創(chuàng)建子進程
execArgv 傳遞給可執(zhí)行字符串參數(shù)數(shù)組列表(默認值:process.execArgv)
silent Boolean - 如果為true,標準輸入,stdout和子標準錯誤將通過管道輸送到父進程,否則會從父繼承,看到“pipe”和“inherit”選項spawn()更多細節(jié)標準輸入輸出(默認為false)
uid 數(shù)字 - 設置進程的用戶的身份。
gid 數(shù)字 - 設置進程的組標識。
fork 方法返回與對象內置除了具有在正常ChildProcess實例的所有方法的通信信道。
創(chuàng)建兩個JS文件名為support.js和master.js:
File: support.js
console.log("Child Process " + process.argv[2] + " executed." );
File: master.js
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 exited with code ' + code);
});
}
現(xiàn)在運行master.js看到的結果:
$ node master.js
驗證輸出。服務器已經(jīng)啟動
Child Process 0 executed. Child Process 1 executed. Child Process 2 executed. child process exited with code 0 child process exited with code 0 child process exited with code 0