国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

node的process以及child_process模塊學(xué)習(xí)筆記

2019-11-19 14:13:53
字體:
供稿:網(wǎng)友

在死磕進(jìn)程一個禮拜后,終于把晦澀難懂文檔看明白了,準(zhǔn)備把自己的理解分享給大家,也希望大家能指出一些意見

進(jìn)程的概念

  1. 在Node.js中每個應(yīng)用程序都是一個進(jìn)程類的實例對象。
  2. 使用process對象代表應(yīng)用程序,這是一個全局對象,可以通過它來獲取Node.jsy應(yīng)用程序以及運(yùn)行該程序的用戶、環(huán)境等各種信息的屬性、方法和事件。

進(jìn)程中幾個重要的屬性

  1. stdin 標(biāo)準(zhǔn)輸入可讀流
  2. stdout 標(biāo)準(zhǔn)輸入可寫流
  3. stderr 標(biāo)準(zhǔn)錯誤輸出流
  4. argv 終端輸入?yún)?shù)數(shù)組
  5. env 操作系統(tǒng)環(huán)境信息
  6. pid 應(yīng)用程序進(jìn)程id

stdin以及stdout

process.stdin.on('data', (chunk) => { process.stdout.write('進(jìn)程接收到數(shù)據(jù)' + chunk)})

運(yùn)行結(jié)果

argv

console.log(process.env)

env: 在mac終端輸入 export NODE_ENV=develop

console.log(process.env.NODE_ENV) //develop

進(jìn)程的方法

  1. process.memoryUsage() 查看內(nèi)存使用信息
  2. process.nextTick() 當(dāng)前eventloop執(zhí)行完畢執(zhí)行回調(diào)函數(shù)
  3. process.chdir() chdir方法用于修改Node.js應(yīng)用程序中使用的當(dāng)前工作目錄
  4. process.cwd() 進(jìn)程當(dāng)前工作目錄
  5. process.kill() 殺死進(jìn)程
  6. process.uncaughtException() 當(dāng)應(yīng)用程序拋出一個未被捕獲的異常時觸發(fā)進(jìn)程對象的uncaughtException事件
say() //方法不存在process.on('uncaughtException',function(err){ console.log('捕獲到一個未被處理的錯誤:',err);});

child_process

子進(jìn)程是今天要講的重點(diǎn),我也有一些不太明白,希望能和大家多多交流

child_process出現(xiàn)的背景

在Node.js中,只有一個線程執(zhí)行所有操作,如果某個操作需要大量消耗CPU資源的情況下,后續(xù)操作都需要等待。

在Node.js中,提供了一個child_process模塊,通過它可以開啟多個子進(jìn)程,在多個子進(jìn)程之間可以共享內(nèi)存空間,可以通過子進(jìn)程的互相通信來實現(xiàn)信息的交換。

child_process模塊給予node任意創(chuàng)建子進(jìn)程的能力,node官方文檔對于child_proces模塊給出了四種方法,映射到操作系統(tǒng)其實都是創(chuàng)建子進(jìn)程。但對于開發(fā)者而已,這幾種方法的api有點(diǎn)不同

child_process.exec(command[, options][, callback]) 啟動
子進(jìn)程來執(zhí)行shell命令,可以通過回調(diào)參數(shù)來獲取腳本shell執(zhí)行結(jié)果

child_process.execfile(file[, args][, options][, callback])
與exec類型不同的是,它執(zhí)行的不是shell命令而是一個可執(zhí)行文件

child_process.spawn(command[, args][, options])僅僅執(zhí)行一個shell命令,不需要獲取執(zhí)行結(jié)果

child_process.fork(modulePath[, args][, options])可以用node
執(zhí)行的.js文件,也不需要獲取執(zhí)行結(jié)果。fork出來的子進(jìn)程一定是node進(jìn)程

spawn

語法:child_process.spawn(command, [args], [options])

  1. command 必須指定的參數(shù),指定需要執(zhí)行的命令
  2. args 數(shù)組,存放了所有運(yùn)行該命令需要的參數(shù)
  3. options 參數(shù)為一個對象,用于指定開啟子進(jìn)程時使用的選項
const { spawn } = require('child_process')const path = require('path')let child1 = spawn('node', ['test1.js', 'yanyongchao'], { stdio: ['pipe', 'pipe', 'pipe'], // 三個元素數(shù)組 下面會詳解 cwd: __dirname, 子進(jìn)程工作目錄 env: process.env, 環(huán)境變量 detached: true // 如果為true,當(dāng)父進(jìn)程不存在時也可以獨(dú)立存在})

其實上面都好理解除了sdtio數(shù)組,下面來一起分析stdio

stdio

stdio是一個數(shù)組,用來設(shè)置標(biāo)準(zhǔn)輸入,標(biāo)準(zhǔn)輸出,錯誤輸出。個人理解

pipe:父進(jìn)程和子進(jìn)程之間建立一個管道

主進(jìn)程代碼

const path = require('path')const { spawn } = require('child_process')let p = spawn('node', ['childs_t.js'], { cwd: path.join(__dirname, 'childs'), stdio: ['pipe', 'pipe', process.stderr]})p.stdout.on('data', (data) => { console.log(data.toString())}) // 這里用stdout原因: 子進(jìn)程的數(shù)據(jù)流與常規(guī)理解的數(shù)據(jù)流方向相反,// stdin:寫入流,stdout、stderr:讀取流。

子進(jìn)程代碼

process.stdout.write('asd')

如果在stdio中放一個流,process.stdout,process.stdin

主進(jìn)程代碼

const { spawn } = require('child_process')const path = require('path')// 如果放的是一個流,則意味著父進(jìn)程和子進(jìn)程共享一個流const p = spawn('node', ['child_t.js'], { cwd: path.join(__dirname, 'childs'), stdio: [process.stdin, process.stdout, process.stderr]})

子進(jìn)程代碼

process.stdout.write('asd') //控制臺會輸出asd

ipc

主進(jìn)程代碼

const path = require('path')const { spawn } = require('child_process')let p = spawn('node', ['child_t.js'], { cwd: path.join(__dirname, 'childs'), stdio: ['ipc', 'pipe', 'pipe']})p.on('message', (msg) => { console.log(msg)})p.send('hello chhild_process')

子進(jìn)程代碼

process.on('message', (msg) => { process.send('子進(jìn)程' + msg)})// child.send(message,[sendHandle]);//在父進(jìn)程中向子進(jìn)程發(fā)送消息// process.send(message,[sendHandle]);//在子進(jìn)程中向主進(jìn)程發(fā)送消息

detached模式

const { spawn } = require('child_process')const fs = require('fs')const path = require('path')let out = fs.openSync(path.join(__dirname, 'childs/msg.txt'), 'w', 0o666)let p = spawn('node', ['test4.js'], { detached: true, //保證父進(jìn)程結(jié)束,子進(jìn)程仍然可以運(yùn)行 stdio: 'ignore', cwd: path.join(__dirname, 'childs')})p.unref()p.on('close', function() { console.log('子進(jìn)程關(guān)閉')})p.on('exit', function() { console.log('子進(jìn)程退出')})p.on('error', function(err) { console.log('子進(jìn)程1開啟失敗' + err)})

fork開啟一個子進(jìn)程

  1. 衍生一個新的 Node.js 進(jìn)程,并通過建立一個 IPC 通訊通道來調(diào)用一個指定的模塊,該通道允許父進(jìn)程與子進(jìn)程之間相互發(fā)送信息
  2. fork方法返回一個隱式創(chuàng)建的代表子進(jìn)程的ChildProcess對象
  3. 子進(jìn)程的輸入/輸出操作執(zhí)行完畢后,子進(jìn)程不會自動退出,必須使用process.exit()方法顯式退出

子進(jìn)程代碼

const { fork } = require('child_process')const path = require('path')let child = fork(path.join(__dirname, 'childs/fork1.js'))child.on('message', (data) => { console.log('父進(jìn)程接收到消息' + data)})child.send('hello fork')child.on('error', (err) => { console.error(err)})

子進(jìn)程代碼

process.on('message', (m, setHandle) => { console.log('子進(jìn)程接收到消息' + m) process.send(m) //sendHandle是一個 net.Socket 或 net.Server 對象})

exec開啟子進(jìn)程

// exec同步執(zhí)行一個shell命令let { exec } = require('child_process')let path = require('path')// 用于使用shell執(zhí)行命令, 同步方法let p1 = exec('node exec.js a b c', {cwd: path.join(__dirname, 'childs')}, function(err, stdout, stderr) { console.log(stdout)})

execFile開啟子進(jìn)程

let { execFile } = require('child_process')let path = require('path')let p1 = execFile('node', ['exec.js', 'a', 'b', 'c'], { cwd: path.join(__dirname, 'childs')}, function(err, stdout, stderr) { console.log(stdout)})

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 金昌市| 昌黎县| 定陶县| 望城县| 无极县| 大竹县| 手机| 景洪市| 盘山县| 安乡县| 元谋县| 亚东县| 屏南县| 博爱县| 龙江县| 安福县| 吐鲁番市| 承德市| 襄樊市| 荆州市| 吴堡县| 射洪县| 睢宁县| 曲松县| 威海市| 牟定县| 视频| 广丰县| 三明市| 佛冈县| 六枝特区| 霍邱县| 嘉善县| 福建省| 阜新市| 威信县| 乌鲁木齐市| 鄱阳县| 渝中区| 兰西县| 遂溪县|