背景
由于ons(阿里云 RocketMQ 包)基于 C艸 封裝而來,不支持單一進(jìn)程內(nèi)實例化多個生產(chǎn)者與消費者,為了解決這一問題,使用了 Node.js 子進(jìn)程。
在使用的過程中碰到的坑
發(fā)布:進(jìn)程管理關(guān)閉主進(jìn)程后,子進(jìn)程變?yōu)椴僮飨到y(tǒng)進(jìn)程(pid 為 1)
幾種解決方案
將子進(jìn)程看做獨立運行的進(jìn)程,記錄 pid,發(fā)布時進(jìn)程管理關(guān)閉主進(jìn)程同時關(guān)閉子進(jìn)程
主進(jìn)程監(jiān)聽關(guān)閉事件,主動關(guān)閉從屬于自己的子進(jìn)程
子進(jìn)程種類
spawn:執(zhí)行命令
exec:執(zhí)行命令(新建 shell)
execFile:執(zhí)行文件
fork:執(zhí)行文件
子進(jìn)程常用事件
exit
close
error
message
close 與 exit 是有區(qū)別的,close 是在數(shù)據(jù)流關(guān)閉時觸發(fā)的事件,exit 是在子進(jìn)程退出時觸發(fā)的事件。因為多個子進(jìn)程可以共享同一個數(shù)據(jù)流,所以當(dāng)某個子進(jìn)程 exit 時不一定會觸發(fā) close 事件,因為這個時候還存在其他子進(jìn)程在使用數(shù)據(jù)流。
子進(jìn)程數(shù)據(jù)流
stdin
stdout
stderr
因為是以主進(jìn)程為出發(fā)點,所以子進(jìn)程的數(shù)據(jù)流與常規(guī)理解的數(shù)據(jù)流方向相反,stdin:寫入流,stdout、stderr:讀取流。
spawn
spawn(command[, args][, options])
執(zhí)行一條命令,通過 data 數(shù)據(jù)流返回各種執(zhí)行結(jié)果。
基礎(chǔ)使用
const { spawn } = require('child_process'); const child = spawn('find', [ '.', '-type', 'f' ]); child.stdout.on('data', (data) => { console.log(`child stdout:\n${data}`); }); child.stderr.on('data', (data) => { console.error(`child stderr:\n${data}`); }); child.on('exit', (code, signal) => { console.log(`child process exit with: code $[code], signal: ${signal}`); });
常用參數(shù)
{ cwd: String, env: Object, stdio: Array | String, detached: Boolean, shell: Boolean, uid: Number, gid: Number }
重點說明下 detached 屬性,detached 設(shè)置為 true 是為子進(jìn)程獨立運行做準(zhǔn)備。子進(jìn)程的具體行為與操作系統(tǒng)相關(guān),不同系統(tǒng)表現(xiàn)不同,Windows 系統(tǒng)子進(jìn)程會擁有自己的控制臺窗口,POSIX 系統(tǒng)子進(jìn)程會成為新進(jìn)程組與會話負(fù)責(zé)人。
這個時候子進(jìn)程還沒有完全獨立,子進(jìn)程的運行結(jié)果會展示在主進(jìn)程設(shè)置的數(shù)據(jù)流上,并且主進(jìn)程退出會影響子進(jìn)程運行。當(dāng) stdio 設(shè)置為 ignore 并調(diào)用 child.unref(); 子進(jìn)程開始真正獨立運行,主進(jìn)程可獨立退出。
exec
exec(command[, options][, callback])
執(zhí)行一條命令,通過回調(diào)參數(shù)返回結(jié)果,指令未執(zhí)行完時會緩存部分結(jié)果到系統(tǒng)內(nèi)存。
const { exec } = require('child_process'); exec('find . -type f | wc -l', (err, stdout, stderr) => { if (err) { console.error(`exec error: ${err}`); return; } console.log(`Number of files ${stdout}`); });
兩全其美 —— spawn 代替 exec
由于 exec 的結(jié)果是一次性返回,在返回前是緩存在內(nèi)存中的,所以在執(zhí)行的 shell 命令輸出過大時,使用 exec 執(zhí)行命令的方式就無法按期望完成我們的工作,這個時候可以使用 spawn 代替 exec 執(zhí)行 shell 命令。
const { spawn } = require('child_process'); const child = spawn('find . -type f | wc -l', { stdio: 'inherit', shell: true }); child.stdout.on('data', (data) => { console.log(`child stdout:\n${data}`); }); child.stderr.on('data', (data) => { console.error(`child stderr:\n${data}`); }); child.on('exit', (code, signal) => { console.log(`child process exit with: code $[code], signal: ${signal}`); });
execFile
child_process.execFile(file[, args][, options][, callback])
執(zhí)行一個文件
與 exec 功能基本相同,不同之處在于執(zhí)行給定路徑的一個腳本文件,并且是直接創(chuàng)建一個新的進(jìn)程,而不是創(chuàng)建一個 shell 環(huán)境再去運行腳本,相對更輕量級更高效。但是在 Windows 系統(tǒng)中如 .cmd 、 .bat 等文件無法直接運行,這是 execFile 就無法工作,可以使用 spawn、exec 代替。
fork
child_process.fork(modulePath[, args][, options])
執(zhí)行一個 Node.js 文件
// parent.js const { fork } = require('child_process'); const child = fork('child.js'); child.on('message', (msg) => { console.log('Message from child', msg); }); child.send({ hello: 'world' });
// child.js process.on('message', (msg) => { console.log('Message from parent:', msg); }); let counter = 0; setInterval(() => { process.send({ counter: counter++ }); }, 3000);
fork 實際是 spawn 的一種特殊形式,固定 spawn Node.js 進(jìn)程,并且在主子進(jìn)程間建立了通信通道,讓主子進(jìn)程可以使用 process 模塊基于事件進(jìn)行通信。
子進(jìn)程使用場景
計算密集型系統(tǒng)
前端構(gòu)建工具利用多核 CPU 并行計算,提升構(gòu)建效率
進(jìn)程管理工具,如:PM2 中部分功能
上面是我整理給大家的,希望今后會對大家有幫助。
相關(guān)文章:
在vue.js中有關(guān)vue-fontawesome使用
使用JS添加元素新節(jié)點
有關(guān)Express中l(wèi)og4js實際用法
在vue中如何引用阿里字體圖標(biāo)的方法
通過Node.js使用MySQL連接池
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com