利用node.js如何創(chuàng)建子進(jì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如何實(shí)現(xiàn)注冊(cè)郵箱激活功能 (常見(jiàn))
今天了解了node如何實(shí)現(xiàn)郵箱激活功能,這個(gè)功能非常常見(jiàn),當(dāng)我們注冊(cè)一個(gè)賬號(hào)時(shí),肯定會(huì)有這步,下面看下如何實(shí)現(xiàn)這個(gè)功能2017-07-07基于nodejs的微信JS-SDK簡(jiǎn)單應(yīng)用實(shí)現(xiàn)
這篇文章主要介紹了基于nodejs的微信JS-SDK簡(jiǎn)單應(yīng)用實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05快速掌握Node.js之Window下配置NodeJs環(huán)境
快速掌握Node.js之Window下配置NodeJs環(huán)境,如何在Window下快速配置NodeJs環(huán)境,感興趣的小伙伴們可以參考一下2016-03-03使用Raygun對(duì)Node.js應(yīng)用進(jìn)行錯(cuò)誤處理的方法
這篇文章主要介紹了使用Raygun對(duì)Node.js應(yīng)用進(jìn)行錯(cuò)誤處理的方法,Node.js是一款用于服務(wù)器端的JavaScript框架,需要的朋友可以參考下2015-06-06

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

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

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

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

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