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

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

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

前言

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

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

child_process模塊提供了一個(gè)ChildProcess的新類(lèi),它可以作為從父進(jìn)程訪問(wèn)子進(jìn)程的表示形式。Process模塊也是ChildProcess對(duì)象。當(dāng)你從父模塊訪問(wèn)process時(shí),它是父ChildProcess對(duì)象,當(dāng)你從子進(jìn)程訪問(wèn)Process是,它是ChildProcess對(duì)象

了解一個(gè)對(duì)象無(wú)外乎事件、方法、屬性。ChildProcess也是一樣。

每個(gè)子進(jìn)程總帶有三個(gè)流對(duì)象:child.stdin、child.stdout、child.stderr。他們可能會(huì)共享父進(jìn)程的stdio流。

這里我們先介紹利用child_process模塊中exec、spawn、fork三個(gè)方法對(duì)子進(jìn)程的操作。

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

其中就一行代碼如下:

console.log("進(jìn)程 " + process.argv[2] + " 執(zhí)行。" );
//換成下面的查看process.argv
//console.log("進(jìn)程 " + 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++) {
 //這里有空格請(qǐng)注意。分別代表node路徑 node-childPro.js路徑 i第幾個(gè)進(jìn)程。 node-childPro.js中的process.argv可以獲取這些信息值
 var childProcess = child_process.exec('node node-childPro.js '+i,
 // 回調(diào)函數(shù) 子進(jìn)程的輸出以回調(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('子進(jìn)程已退出,退出碼 ' + code);
 });
}

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

G:\node\node-childProcess> node node-childPro-exec.js
子進(jìn)程已退出,退出碼 0
stdout: 進(jìn)程 0 執(zhí)行。
stderr:
子進(jìn)程已退出,退出碼 0
stdout: 進(jìn)程 1 執(zhí)行。
stderr:
子進(jìn)程已退出,退出碼 0
stdout: 進(jìn)程 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('子進(jìn)程已退出,退出碼 '+code);
 });
}

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

G:\node\node-childProcess> node node-childPro-spawn.js
stdout: 進(jìn)程 0 執(zhí)行。
子進(jìn)程已退出,退出碼 0
stdout: 進(jìn)程 1 執(zhí)行。
stdout: 進(jìn)程 2 執(zhí)行。
子進(jìn)程已退出,退出碼 0
子進(jìn)程已退出,退出碼 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('子進(jìn)程已退出,退出碼 ' + code);
 });
}

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

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

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

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

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

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

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

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

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

總結(jié)

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

相關(guān)文章

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

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

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

    node.js中的buffer.length方法使用說(shuō)明

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

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

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

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

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

    Nodejs文件上傳、監(jiān)聽(tīng)上傳進(jìn)度的代碼

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