簡(jiǎn)介
node.js是基于單線程模型架構(gòu)窑多,這樣可以帶來(lái)高效的CPU利用率蟋座,但是無(wú)法卻利用多核心的CPU言询,為了解決這個(gè)問(wèn)題亲善,node.js提供了child_process模塊,通過(guò)多進(jìn)程來(lái)實(shí)現(xiàn)對(duì)多核CPU的利用闷板。
child_process這個(gè)模塊非常重要澎灸,掌握了它,等于在node的世界開啟了一扇新的大門遮晚。熟悉shell腳本的同學(xué)性昭,可以用它來(lái)完成很多有意思的事情,比如文件壓縮县遣、增量部署等糜颠。
child_process模塊提供了四個(gè)創(chuàng)建子進(jìn)程的函數(shù),分別是spawn萧求,exec其兴,execFile和fork。.exec()夸政、.execFile()元旬、.fork()底層都是通過(guò).spawn()實(shí)現(xiàn)的,而且都有對(duì)應(yīng)的同步版本守问。.exec()匀归、execFile()額外提供了回調(diào),當(dāng)子進(jìn)程停止的時(shí)候執(zhí)行耗帕。
- 創(chuàng)建異步進(jìn)程穆端。 在 Windows 上衍生 .bat 和 .cmd 文件
- child_process.exec(command[, options][, callback])
- child_process.execFile(file[, args][, options][, callback])
- child_process.fork(modulePath[, args][, options])
- child_process.spawn(command[, args][, options])
- options.detached
- options.stdio
- 創(chuàng)建同步進(jìn)程
- child_process.execFileSync(file[, args][, options])
- child_process.execSync(command[, options])
- child_process.spawnSync(command[, args][, options])
exec
創(chuàng)建一個(gè)shell,然后在shell里執(zhí)行命令仿便。執(zhí)行完成后徙赢,將error、stdout探越、stderr作為參數(shù)傳入回調(diào)方法狡赐。
var exec = require('child_process').exec;//return a Function
console.log(exec)// [Function]
// 成功的例子
var execProcess = exec('mkdir delete & cd ./delete & touch 1.txt & ls -alh', function (error, stdout, stderr) {
if (error) {
console.error('error: ' + error);
return;
}
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr.length);
});
console.log('--------------', execProcess.constructor.name)// ChildProcess
// execProcess.stdout.on('data', (data) => {
// console.log(`on stdout: ${data}`);
// });回調(diào)中已經(jīng)打印了
// execProcess.stderr.on('data', (data) => {
// console.log(`on stderr: ${data}`);
// });回調(diào)中已經(jīng)打印了
execProcess.on('close', code => {
console.log(`child process exited with code ${code}`);
})
console.log('*****************************************************')
// 失敗的例子
exec('ls hwgaeefewlweglo.txt', function (error, stdout, stderr) {
if (error) {
console.error('error: ' + error);
return;
}
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
});
execFile
跟.exec()類似,不同點(diǎn)在于钦幔,沒(méi)有創(chuàng)建一個(gè)新的shell枕屉。file: 可執(zhí)行文件的名字,或者路徑鲤氢。至少有兩個(gè)特點(diǎn):
- 比child_process.exec()效率高一些搀擂。(實(shí)際待測(cè)試)
- 一些操作,比如I/O重定向卷玉,文件glob等不支持哨颂。
var execFile = require('child_process').execFile;
execFile('node', ['--version'], function (error, stdout, stderr) {
if (error) {
throw error;
}
console.log('stdout: ', stdout);
console.log('stderr: ', stderr);
});
execFile('ls', ['-alh'], function (error, stdout, stderr) {
if (error) {
throw error;
}
console.log(stdout);
});
child_process.spawn(command[, args][, options])
var spawn = require('child_process').spawn;
var ls = spawn('ls', ['-al']);
ls.stdout.on('data', function(data){
console.log('data from child: ' + data);
});
ls.stderr.on('data', function(data){
console.log('error from child: ' + data);
});
ls.on('close', function(code){
console.log('child exists with code: ' + code);
});
child_process.fork(modulePath[, args][, options])
modulePath
<string> 要在子進(jìn)程中運(yùn)行的模塊。args
<Array> 字符串參數(shù)列表相种。-
options
<Object>-
cwd
<string> 子進(jìn)程的當(dāng)前工作目錄威恼。 -
env
<Object> 環(huán)境變量鍵值對(duì)。 -
execPath
<string> 用來(lái)創(chuàng)建子進(jìn)程的執(zhí)行路徑。 -
execArgv
<Array> 要傳給執(zhí)行路徑的字符串參數(shù)列表箫措。默認(rèn)為process.execArgv
腹备。 -
silent
<boolean> 如果為true
,則子進(jìn)程中的 stdin斤蔓、 stdout 和 stderr 會(huì)被導(dǎo)流到父進(jìn)程中植酥,否則它們會(huì)繼承自父進(jìn)程,詳見child_process.spawn()
的stdio
中的'pipe'
和'inherit'
選項(xiàng)弦牡。 默認(rèn):false
友驮。 -
stdio
<Array> | <string> 詳見child_process.spawn()
的stdio
。 當(dāng)提供了該選項(xiàng)驾锰,則它會(huì)覆蓋silent
喊儡。 如果使用了數(shù)組變量,則該數(shù)組必須包含一個(gè)值為'ipc'
的子項(xiàng)稻据,否則會(huì)拋出錯(cuò)誤艾猜。 例如[0, 1, 2, 'ipc']
。 -
windowsVerbatimArguments
<boolean> 決定在Windows系統(tǒng)下是否使用轉(zhuǎn)義參數(shù)捻悯。 在Linux平臺(tái)下會(huì)自動(dòng)忽略匆赃。默認(rèn)值:false
。 -
uid
<number> 設(shè)置該進(jìn)程的用戶標(biāo)識(shí)今缚。(詳見setuid(2)
-
gid
<number> 設(shè)置該進(jìn)程的組標(biāo)識(shí)算柳。(詳見setgid(2)
-
返回: <ChildProcess>