欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用node.js如何創(chuàng)建子進程詳解

 更新時間:2017年12月09日 11:19:14   作者:風慕李  
之前看多進程這一章節(jié)時發(fā)現(xiàn)這塊東西挺多,寫Process模塊的時候也有提到,今天下午午休醒來靜下心來好好的看了一遍,發(fā)現(xiàn)也不是太難理解。所以下面這篇文章主要給大家介紹了關(guān)于利用node.js如何創(chuàng)建子進程的相關(guān)資料,需要的朋友可以參考下。

前言

node本身為單進程,并使用驅(qū)動模式處理并發(fā),為了解決單進程在多核cpu上的資源浪費,node提供了cluster和child_process模塊來創(chuàng)建多個子進程。

Node.js是單線程的,對于現(xiàn)在普遍是多處理器的機器是一種浪費,怎么能利用起來呢?于是child_process模塊出現(xiàn)了。child_process模塊可以在其他進程上產(chǎn)生、派生,并執(zhí)行工作。

child_process模塊提供了一個ChildProcess的新類,它可以作為從父進程訪問子進程的表示形式。Process模塊也是ChildProcess對象。當你從父模塊訪問process時,它是父ChildProcess對象,當你從子進程訪問Process是,它是ChildProcess對象

了解一個對象無外乎事件、方法、屬性。ChildProcess也是一樣。

每個子進程總帶有三個流對象:child.stdin、child.stdout、child.stderr。他們可能會共享父進程的stdio流。

這里我們先介紹利用child_process模塊中exec、spawn、fork三個方法對子進程的操作。

建立node-childProcess文件,在其中創(chuàng)建node-childPro.js文件。

其中就一行代碼如下:

console.log("進程 " + process.argv[2] + " 執(zhí)行。" );
//換成下面的查看process.argv
//console.log("進程 " + process.argv + " 執(zhí)行。" );

exec()方法

在node-childProcess文件中新建node-childPro-exec.js文件,其中代碼如下:

const fs = require('fs');
const child_process = require('child_process');
for (var i = 0; i < 3; i++) {
 //這里有空格請注意。分別代表node路徑 node-childPro.js路徑 i第幾個進程。 node-childPro.js中的process.argv可以獲取這些信息值
 var childProcess = child_process.exec('node node-childPro.js '+i,
 // 回調(diào)函數(shù) 子進程的輸出以回調(diào)函數(shù)參數(shù)的形式返回
 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);
 });
 childProcess.on('exit', function (code) {
 console.log('子進程已退出,退出碼 ' + code);
 });
}

終端執(zhí)行代碼結(jié)果如下:

G:\node\node-childProcess> node node-childPro-exec.js
子進程已退出,退出碼 0
stdout: 進程 0 執(zhí)行。
stderr:
子進程已退出,退出碼 0
stdout: 進程 1 執(zhí)行。
stderr:
子進程已退出,退出碼 0
stdout: 進程 2 執(zhí)行。
stderr:

spawn()方法

在node-childProcess文件中新建node-childPro-spawn.js,其中代碼如下:

const fs = require('fs');
const child_process = require('child_process');
 for(var i=0; i<3; i++) {
 var childProcess = child_process.spawn('node', ['node-childPro-spawn.js', i]);  
 childProcess.stdout.on('data', function (data) {
 console.log('stdout: ' + data);
 });
childProcess.stderr.on('data', function (data) {
 console.log('stderr: ' + data);
 });
 childProcess.on('close', function (code) {
 console.log('子進程已退出,退出碼 '+code);
 });
}

終端執(zhí)行代碼結(jié)果如下:

G:\node\node-childProcess> node node-childPro-spawn.js
stdout: 進程 0 執(zhí)行。
子進程已退出,退出碼 0
stdout: 進程 1 執(zhí)行。
stdout: 進程 2 執(zhí)行。
子進程已退出,退出碼 0
子進程已退出,退出碼 0

fork()方法

在node-childProcess文件中新建node-childPro-fork.js,其中代碼如下:

const fs = require('fs');
const child_process = require('child_process');
 for(var i=0; i<3; i++) {
 var childProcess = child_process.fork("node-childPro.js", [i]); 
 childProcess.on('close', function (code) {
 console.log('子進程已退出,退出碼 ' + code);
 });
}

終端執(zhí)行代碼結(jié)果如下:

G:\node\node-childProcess> node node-childPro-fork.js
進程 0 執(zhí)行。
進程 1 執(zhí)行。
子進程已退出,退出碼 0
進程 2 執(zhí)行。
子進程已退出,退出碼 0
子進程已退出,退出碼 0

關(guān)于exec、spawn、fork

     1.exec函數(shù)是對spawn的一種友好封裝,增加Shell命令解析,可以直接嵌入復雜的命令

     2.exec函數(shù)緩存子進程的輸出,并將子進程的輸出以回調(diào)函數(shù)參數(shù)的形式返回

     3.spawn在子線程開始執(zhí)行后,就開始不斷將數(shù)據(jù)從子進程返回給主進程(應(yīng)用場景如“系統(tǒng)監(jiān)控”)

     4.spawn是不支持callback函數(shù)的,它通過流的方式發(fā)數(shù)據(jù)傳給主進程,從而實現(xiàn)了多進程之間的數(shù)據(jù)交換

     5.fork()是spawn()的特殊情景,用于派生Node進程。除了普通ChildProcess實例所具有的所有方法,所返回的對象還具有內(nèi)建的通訊通道。

下載地址:https://gitee.com/wangFengJ/node/tree/master/node-childProcess

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • node.js利用mongoose獲取mongodb數(shù)據(jù)的格式化問題詳解

    node.js利用mongoose獲取mongodb數(shù)據(jù)的格式化問題詳解

    這篇文章主要給大家介紹了關(guān)于node.js利用mongoose獲取mongodb數(shù)據(jù)的格式化問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習把。
    2017-10-10
  • node.js中的buffer.length方法使用說明

    node.js中的buffer.length方法使用說明

    這篇文章主要介紹了node.js中的buffer.length方法使用說明,本文介紹了buffer.length的方法說明、語法、接收參數(shù)、使用實例和實現(xiàn)源碼,需要的朋友可以參考下
    2014-12-12
  • 利用Mongoose讓JSON數(shù)據(jù)直接插入或更新到MongoDB

    利用Mongoose讓JSON數(shù)據(jù)直接插入或更新到MongoDB

    這篇文章主要給大家介紹了利用Mongoose讓JSON數(shù)據(jù)直接插入或更新到MongoDB數(shù)據(jù)庫的相關(guān)資料,文中詳細介紹了配置Mongoose、創(chuàng)建目錄及文件、插入數(shù)據(jù),POST提交JSON增加一條記錄以及詢數(shù)據(jù),取出剛增加的記錄等內(nèi)容,需要的朋友可以參考下。
    2017-05-05
  • Node.js系列之發(fā)起get/post請求(2)

    Node.js系列之發(fā)起get/post請求(2)

    這篇文章主要為大家詳細介紹了Node.js系列之發(fā)起get/post請求,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • Nodejs文件上傳、監(jiān)聽上傳進度的代碼

    Nodejs文件上傳、監(jiān)聽上傳進度的代碼

    這篇文章主要介紹了Nodejs文件上傳、監(jiān)聽上傳進度,本文通過實例代碼給大家詳細介紹,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • 最新評論