1.process是一個全局進程,你可以直接通過process變量直接訪問它。
process實現了EventEmitter接口,exit方法會在當進程退出的時候執行。因為進程退出之后將不再執行事件循環,所有只有那些沒有回調函數的代碼才會被執行。
在下面例子中,setTimeout里面的語句是沒有辦法執行到的。
1 process.on('exit', function () { 2 setTimeout(function () { 3 console.log('This will not run'); 4 }, 100); 5 console.log('exit'); 6 });
//結果打印出exit
2.在你接觸node之后,你就會發現那些影響了主事件循環的異常會把整個node進程宕掉的。
這會是相當嚴重的問題,所以process提供了另外一個有用的事件uncaughtException來解決這個問題,他會把異常抓取出來供你處理。
1 process.on('uncaughtException', function (err) { 2 console.log('Caught exception: ' + err); 3 }); 4 setTimeout(function () { 5 console.log('This will still run.'); 6 }, 500); 7 // Intentionally cause an exception, but don't catch it. 8 nonexistentFunc(); 9 console.log('This will not run.');
我們來看上面的例子,我們注冊了uncaughtException事件來捕捉系統異常。執行到nonexistentFunc()時,因為該函數沒有定義所以會拋出異常。
因為javascript是解釋性的語言,nonexistentFunc()方法上面的語句不會被影響到,他下面的語句不會被執行。所以他的執行結果如下:
Caught exception: ReferenceError: nonexistentFunc is not defined
This will still run.
1.child_process是Node.js的一個十分重要的模塊,通過它可以實現創建多進程,以利用多核計算資源。
child_process
模塊提供了四個創建子進程的函數,分別是spawn
,exec
,execFile
和fork
。其中spawn
是最原始的創建子進程的函數,其他三個都是對spawn
不同程度的封裝。
spawn
只能運行指定的程序,參數需要在列表中給出,相當於execvp
系統函數,而exec
可以直接運行復雜的命令。
child_process.spawn(command, [args], [options])
child_process.exec(command, [options], callback)
例如要運行ls -lh /usr
,使用spawn
需要寫成spawn('ls', ['-lh', '/usr'])
,而exec
只需exec('ls -lh /usr')
。
exec
的實現原理是啟動了一個系統shell來解析參數,因此可以是非常復雜的命令,包括管道和重定向。
此外,exec
還可以直接接受一個回調函數作為參數,回調函數有三個參數,分別是err
, stdout
, stderr
,非常方便直接使用,例如:
1 require('child_process').exec( 'ls -lh /usr' , function(err, stdout , stderr ) { 2 console.log( stdout ); 3 });
如果使用spawn
,則必須寫成:
2.fork
函數用於直接運行Node.js模塊,例如fork('./child.js')
,相當於spawn('node', ['./child.js'])
。
與默認的spawn
不同的是,fork
會在父進程與子進程直接建立一個IPC管道,用於父子進程之間的通信。例如
1 var n = require('child_process').fork( './child.js'); 2 n. on ( 'message', function(m) { 3 console. log ( 'PARENT got message:', m); 4 }); 5 n.send({ hello: 'world' });
child.js的內容
1 process. on ( 'message', function(m) { 2 console. log ( 'CHILD got message:', m); 3 }); 4 process.send({ foo: 'bar' });
結果:
1 PARENT got message: { foo: 'bar' } 2 CHILD got message: { hello: 'world' }
3.fork
函數有一個問題,就是它只能運行JavaScript代碼,如果你喜歡用CoffeeScript(或者其他任何編譯到js的語言),是無法通過fork
調用的。
一個簡單的方法是把代碼編譯到JavaScript再運行,但是很不方便,有沒有什么辦法呢?答案是可以的,
child_process.spawn(command, [args], [options])
通過把options
參數的stdio
設為['ipc']
,
即可在父子進程之間建立IPC管道。例如子進程使用CoffeeScript:
1 child_process = require ('child_process'); 2 options ={stdio: ['ipc'] }; 3 child = child_process.spawn('coffee', ['./child.coffee'], options);
其中只要把spawn
的第一個參數設置為運行對應腳本的解釋器,即可運行,例如使用Continuation.js,
只需child = child_process.spawn('continuation', ['./child.coffee'], options)
。
參考:
http://www.cnblogs.com/yuyutianxia/p/3271733.html