node js fork php,Node.js中execFile,spawn,exec和fork简介
Node.js中execFile,spawn,exec和fork簡介
Node.js子流程child_process模塊提供四種不同方法執(zhí)行外部應(yīng)用:
所有這些都是異步,調(diào)用這些方法會返回一個對象,這對象是ChildProcess類的實例。
1. execFile
用于執(zhí)行一個外部應(yīng)用,應(yīng)用退出后會返回一些可選蠶食和帶有緩沖輸出的callback。
child_process.execFile(file[, args][, options][, callback])
文件?將要執(zhí)行外部應(yīng)用的可執(zhí)行文件的路徑或名稱
參數(shù)?是字符串?dāng)?shù)組
可選?
cwd??子流程當(dāng)前工作目錄
env??key-value環(huán)境值對
encoding??(默認(rèn): ‘utf8’)
timeout??(Default: 0)
maxBuffer??標(biāo)準(zhǔn)輸入或輸出的大量數(shù)據(jù) (in bytes) – 如果超過子流程會被殺死 (Default:200\*1024)
killSignal??(Default: ‘SIGTERM’)
uid??流程的用戶標(biāo)識符 (See?setuid(2).)
gid??流程的群組標(biāo)識符(See?setgid(2).)
當(dāng)外部應(yīng)用存在時,Node程序?qū)y帶參數(shù)"-version"被執(zhí)行,當(dāng)外部應(yīng)用退出時,回調(diào)函數(shù)被調(diào)用,回調(diào)函數(shù)帶有子流程的標(biāo)準(zhǔn)輸入輸出,來自外部應(yīng)用的標(biāo)準(zhǔn)輸出將被內(nèi)部緩沖保存。
運(yùn)行下面代碼將打印當(dāng)前node版本:
const execFile = require( 'child_process').execFile;
const child = execFile( 'node', [ '--version'], ( error, stdout, stderr) => {
if ( error) {
console.error( 'stderr', stderr);
throw error;
}
console.log( 'stdout', stdout);
});
node是如何發(fā)現(xiàn)外部應(yīng)用?它是由PATH環(huán)境變量,其中會指定一系列目錄,可執(zhí)行的外部應(yīng)用駐留在這些目錄中,如果外部應(yīng)該被發(fā)現(xiàn)存在,無需該外部應(yīng)用的絕對路徑或相對路徑就會被定位。
execFile是當(dāng)需要執(zhí)行外部應(yīng)用并獲得輸出時使用,我們能使用它運(yùn)行一個圖片處理應(yīng)用將圖片從PNG轉(zhuǎn)為JPG格式,我們只關(guān)心其成功與否,當(dāng)外部應(yīng)用產(chǎn)生大量數(shù)據(jù)以及我們需要實時使用這些數(shù)據(jù)時,execFile就不要使用。
2.spawn
spawn?方法會在新的流程執(zhí)行外部應(yīng)用,返回I/O的一個流接口。
child_process.spawn(command[, args][, options])
使用案例:
const spawn = require( 'child_process').spawn;
const fs = require( 'fs');
function resize( req, resp) {
const args = [
"-", // use stdin
"-resize", "640x", // resize width to 640
"-resize", "x360
"-gravity", "center", // sets the offset to the center
"-crop", "640x360+0+0", // crop
"-" // output to stdout
];
const streamIn = fs.createReadStream( './path/to/an/image');
const proc = spawn( 'convert', args);
streamIn.pipe( proc.stdin);
proc.stdout.pipe( resp);
}
上面是一個express.js控制器函數(shù)中代碼,我們使用流從一個圖片文件讀取,然后使用spawn方法生成convert程序,我們使用圖片流喂給ChildProcess proc,只要這個proc對象產(chǎn)生數(shù)據(jù),我們寫入數(shù)據(jù)到一個可寫的流中,用戶無需等待整個圖片轉(zhuǎn)換完畢就能立即看見圖片。
spawn返回一個對象流,對于輸出大量數(shù)據(jù)然后需要讀取的應(yīng)用適合,因為是基于流,所有流好處有:
低內(nèi)存損耗
自動處理后壓back-pressure
在緩沖塊中懶生產(chǎn)或消費數(shù)據(jù)
基于事件且非堵塞
緩沖可以讓你超過V8 heap內(nèi)存限制
3.exec
這個方法將會生成一個子shell,能夠在shell中執(zhí)行命令,并緩沖產(chǎn)生的數(shù)據(jù),當(dāng)子流程完成后回調(diào)函數(shù)將會被調(diào)用,可帶有:
當(dāng)命令成功執(zhí)行,緩沖的數(shù)據(jù)
當(dāng)命令失敗,錯誤信息
child_process.exec(command[, options][, callback])
與execFile 和 spawn相比,exec并沒有參數(shù),因為exec允許我們在shell中執(zhí)行多個命令,當(dāng)使用exec時,我們?nèi)绻枰獋鬏攨?shù)到命令行,它們應(yīng)該作為整個命令字符串的一部分。
下面代碼將會輸出當(dāng)前目錄下所有遞歸條目:
const exec = require( 'child_process').exec;
exec( 'for i in $( ls -LR ); do echo item: $i; done', ( e, stdout, stderr)=> {
if ( e instanceof Error) {
console.error( e);
throw e;
}
console.log( 'stdout ', stdout);
console.log( 'stderr ', stderr);
});
當(dāng)在一個shell中運(yùn)行命令,我們能實現(xiàn)在shell中所有功能,比如管道 重定向:
const exec = require( 'child_process').exec;
exec( 'netstat -aon | find "9000"', ( e, stdout, stderr)=> {
if ( e instanceof Error) {
console.error( e);
throw e;
}
console.log( 'stdout ', stdout);
console.log( 'stderr ', stderr);
});
上面案例中,node會生成一個子shell,并執(zhí)行命令: netstat -aon | find “9000”
exec只有需要利用shell功能時才能使用。
4.fork
child_process.fork()方法是child_process.spawn()特殊情況,子流程返回一個ChildProcess對象,這個ChildProcess會有附加通訊通道,允許消息在父子之間來回穿梭。fork方法會打開一個IPC通道,允許Node流程之間傳遞消息:
如在子流程,?process.on(‘message’)?和?process.send(‘message to parent’)?能被用來接受和發(fā)送數(shù)據(jù)
入在父流程,?使用child.on(‘message’)?和?child.send(‘message to child’)
每個流程都有自己的內(nèi)存,都有它們自己的V8實例,啟動至少30毫秒,每個大小是10mb
//parent.js
const cp = require( 'child_process');
const n = cp.fork( `${ __dirname }/sub.js`);
n.on( 'message', ( m) => {
console.log( 'PARENT got message:', m);
});
n.send({ hello: 'world' });
//sub.js
process.on( 'message', ( m) => {
console.log( 'CHILD got message:', m);
});
process.send({ foo: 'bar' });
因為Node主流程是單線程的,長期運(yùn)行任務(wù)如計算等會堵塞主流程,因此,進(jìn)來的請求不能被處理,應(yīng)用變得不可響應(yīng),將這些長期運(yùn)行任務(wù)放入主流程之外運(yùn)行,fork一個新的node流程專門處理,這樣主流程能夠進(jìn)行處理進(jìn)來的請求保持可響應(yīng)性。
總結(jié)
以上是生活随笔為你收集整理的node js fork php,Node.js中execFile,spawn,exec和fork简介的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对数周期天线hfss建模_HFSS也有金
- 下一篇: php程序怎么上传服务器,php本地文件