利用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的新類,它可以作為從父進(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在子線程開始執(zhí)行后,就開始不斷將數(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)文章
Linux下為Node.js程序配置MySQL或Oracle數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Linux下為Node.js程序配置MySQL或Oracle數(shù)據(jù)庫(kù)的方法,這里默認(rèn)已經(jīng)裝配好了Node環(huán)境然后我們利用npm包管理工具來(lái)進(jìn)行配置,需要的朋友可以參考下2016-03-03
node.js安裝及環(huán)境配置超詳細(xì)步驟講解(Windows系統(tǒng)安裝包方式)
這篇文章主要介紹了node.js安裝及環(huán)境配置超詳細(xì)教程(Windows系統(tǒng)安裝包方式),本文分步驟通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
AngularJS + Node.js + MongoDB開發(fā)的基于高德地圖位置的通訊錄
這篇文章主要介紹了AngularJS + Node.js + MongoDB開發(fā)的基于高德地圖位置的通訊錄,需要的朋友可以參考下2015-01-01
淺談node使用jwt生成的token應(yīng)該存在哪里
早上逛某乎的時(shí)候,遇到一位同學(xué)在問(wèn)這個(gè)問(wèn)題,很好奇jwt的存儲(chǔ)位置。本文詳細(xì)的介紹一下,感興趣的可以了解一下2021-06-06
node.js中的buffer.toString方法使用說(shuō)明
這篇文章主要介紹了node.js中的buffer.toString方法使用說(shuō)明,本文介紹了buffer.toString的方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下2014-12-12
nodejs 十六進(jìn)制字符串型數(shù)據(jù)與btye型數(shù)據(jù)相互轉(zhuǎn)換
這篇文章主要介紹了nodejs 十六進(jìn)制字符串型數(shù)據(jù)與btye型數(shù)據(jù)相互轉(zhuǎn)換,需要的朋友可以參考下2018-07-07

