search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

[Node.js] 使用 child_process 模組建立子處理程序

此篇文章瀏覽量: 1,827

child_precess 模組提供了四種建立子處理程序的方法,分別是:spawn()exec()execFile()fork()。為了要讓 Node.js 能夠支援多處理程序任務,就能夠實現對多核心 CPU 的有效利用。

spawn() 是最原始的建立子處理程序的方法,其他三個都是透過對 spawn() 方法不同程度地進一步封裝實作的。

使用 spawn() 方法建立 child process(子處理程序)

var spawn = require('child_process').spawn;
// 執行 ls -lh 指令:
var ls_var = spawn('ls', ['-lh', '/var']);
ls_var.stdout.on('data', function(data){
  console.log('stdout: ' + data);
});

// 監聽 error 事件:
ls_var.on('error', function(code){
  console.log('error with code ' + code);
});

// 監聽 close 事件:
ls_var.on('close', function(code){
  console.log('closed with code ' + code);
});

// 監聽 exit 事件:
ls_var.on('exit', function(code){
  console.log('exited with code ' + code);
});

對於 Node.js 框架中的系統 close 與 exit 事件,在正常退出時的退出碼定義為 0,而在非正常退出時的退出碼均不為 0,通常是定義為 1 ~ 3 的數字。

使用 exec() 方法建立 child process(子處理程序)

var exec = require('child_process').exec;
// 執行 cat child_process.js 指令:
var child = exec('cat child_process.js', function(error, stdout, stderr){
  console.info('cat child_process.js stdout: ');
  console.log(stdout);
});

spawn() 與 exec() 的差別

  • spawn() 方法的參數必須放到 arg 陣列參數中,而不能放到 command 參數裡,也就是說,這些參數都不能帶有空格;而 exec() 方法可以將參數直接放在 command 參數裡。
  • child_process 模組中的 spawn() 方法是「非同步中的非同步」,意思是指在子處理程序開始執行時,它就開始從一個流程將資料從子處理程序傳回給 Node。所以,當想要處理程序傳回大量資料給 Node.js 框架時,最好使用 spawn() 方法。
  • child_process 模組中的 exec() 方法是「同步中的非同步」,意思是儘管 exec 是非同步的,但是它一定要等到子處理程序執行結束以後,再一次性的傳回所有的 buffer 資料。所以如果 exec() 的 buffer 體積設置得不夠大,它將會以一個 “maxBuffer exceeded” 錯誤失敗而告終。

使用 spawn() 方法鎖定系統事件

var cp = require('child_process'); // 載入 child_process 模組
var cat = cp.spawn('cat'); // 使用 cat 指令

// data 事件:截取 console 輸出物件 stdout 的 data 事件
cat.stdout.on('data', function(d){
  console.log(d.toString());
});

// exit 事件:鎖定系統 exit 事件,此事件發生時,子處理程序的標準輸入、輸出(stdio),資料流可能仍為開啟狀態
cat.on('exit', function(){
  console.log("cat on exit!");
});

// close 事件:鎖定系統 close 事件,子處理程序的所有標準輸入、輸出(stdio)資料流被終止時觸發
cat.on('close', function(){
  console.log("cat on close!");
});

// 透過 console 輸入物件 stdin 寫入資料
cat.stdin.write('cat on data!');

// 結束 console 輸入物件 stdin
cat.stdin.end();

使用 exec() 方法鎖定錯誤事件

var exec = require('child_process').exec;
// 執行 cat child_process2.js 指令,且 child_process2.js 檔案是不存在的,所以使用以下寫法來輸出錯誤內容:
var child = exec('cat child_process2.js', function(error, stdout, stderr){
  console.info('cat child_process2.js stdout: ');
  console.log(stderr);
  if(error !== null){
    console.info('cat child_process2.js error: ');
    console.log(error);
  }
});

使用 execFile() 方法建立 child process(子處理程序)

var child_process = require('child_process'); // 載入 child_process 模組
child_process.execFile('/bin/ls', ['-lh', '.'], function(error, stdout, stderr){
  if(error !== null){
    console.log('execFile error: ' + error);
    console.log('execFile stderr: ' + stderr);
  }else{
    console.info('目前的目錄內容:');
    console.info(stdout);
  }
});

exec() 方法與 execFile() 方法均支援 callback,二者的區別就是 execFile() 方法不用啟動獨立的 shell,因此相對來說,較輕量。

使用 fork() 方法取得 CPU 資訊

var fork = require('child_process').fork; // 載入 child_process 模組

// 取得機器現在的 CPU 核心數量
var cpus = require('os').cpus();
console.log('現在機器 CPU 核心數量:' + cpus.length);

for(var i = 0; i < cpus.length; i++){
  // 透過 fork() 方法建立新的 child process
  console.log('Fork a new child_process now...');
  fork('./worker.js'); // 產生新的 child process
}

// worker.js 檔案內容(只有一行):
console.info('This is a child_process.');

資料來源

若覺得文章有幫助,請多分享,讓其他同好也能吸收網站技能知識。



熱門推薦

本文由 carlos-studiocom 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦