process存在于全局對象上,不需要使用require()加載即可使用,process模塊主要做兩方面的事情
資源使用
指運行此進程所消耗的機器資源。例如內存、cpu
內存
process.memoryUsage()){ rss: 21848064, heapTotal: 7159808, heapUsed: 4431688, external: 8224 }
rss(常駐內存)的組成見下圖
code segment對應當前運行的代碼
external對應的是C++對象(與V8管理的JS對象綁定)的占用的內存,比如Buffer的使用
Buffer.allocUnsafe(1024 * 1024 * 1000);console.log(process.memoryUsage());{ rss: 22052864, heapTotal: 6635520, heapUsed: 4161376, external: 1048584224 }
cpu
const startUsage = process.cpuUsage();console.log(startUsage);const now = Date.now();while (Date.now() - now < 500);console.log(process.cpuUsage());console.log(process.cpuUsage(startUsage)); //相對時間// { user: 59459, system: 18966 }// { user: 558135, system: 22312 }// { user: 498432, system: 3333 }
user對應用戶時間,system代表系統時間
運行環境
運行環境指此進程運行的宿主環境包括運行目錄、node環境、CPU架構、用戶環境、系統平臺
運行目錄
const startUsage = process.cpuUsage();console.log(startUsage);const now = Date.now();while (Date.now() - now < 500);console.log(process.cpuUsage());console.log(process.cpuUsage(startUsage)); //相對時間// { user: 59459, system: 18966 }// { user: 558135, system: 22312 }// { user: 498432, system: 3333 }
node環境
console.log(process.version)// v9.1.0
如果不僅僅希望獲得node的版本信息,還希望v8、zlib、libuv版本等信息的話就需要使用process.versions了
console.log(process.versions);{ http_parser: '2.7.0', node: '9.1.0', v8: '6.2.414.32-node.8', uv: '1.15.0', zlib: '1.2.11', ares: '1.13.0', modules: '59', nghttp2: '1.25.0', openssl: '1.0.2m', icu: '59.1', unicode: '9.0', cldr: '31.0.1', tz: '2017b' }
cpu架構
console.log(`This processor architecture is ${process.arch}`);// This processor architecture is x64
支持的值包括:'arm'
, 'arm64'
, 'ia32'
, 'mips'
, 'mipsel'
, 'ppc'
, 'ppc64'
, 's390'
, 's390x'
, 'x32'
'x64'
用戶環境
console.log(process.env.NODE_ENV); // devNODE_ENV=dev node b.js
除了啟動時的自定義信息之外,process.env還可以獲得其他的用戶環境信息(比如PATH、SHELL、HOME等),感興趣的可以自己打印一下試試
系統平臺
console.log(`This platform is ${process.platform}`);This platform is darwin
支持的系統平臺包括:'aix'
'darwin'
'freebsd'
'linux'
'openbsd'
'sunos'
'win32'
android目前還處于試驗階段
運行狀態
運行狀態指當前進程的運行相關的信息包括啟動參數、執行目錄、主文件、PID信息、運行時間
啟動參數
獲取啟動參數有三個方法,execArgv獲取Node.js的命令行選項(見官網文檔)
argv獲取非命令行選項的信息,argv0則獲取argv[0]的值(略有差異)
console.log(process.argv)console.log(process.argv0)console.log(process.execArgv)node --harmony b.js foo=bar --version// 輸出結果[ '/Users/xiji/.nvm/versions/node/v9.1.0/bin/node', '/Users/xiji/workspace/learn/node-basic/process/b.js', 'foo=bar', '--version' ]node[ '--harmony' ]
執行目錄
console.log(process.execPath);// /Users/xxxx/.nvm/versions/node/v9.1.0/bin/node
運行時間
var date = new Date();while(new Date() - date < 500) {}console.log(process.uptime()); // 0.569
主文件
除了require.main之外也可以通過process.mainModule來判斷一個模塊是否是主文件
//a.jsconsole.log(`module A: ${process.mainModule === module}`);//b.jsrequire('./a');console.log(`module B: ${process.mainModule === module}`);node b.js// 輸出module A: falsemodule B: true
PID信息
console.log(`This process is pid ${process.pid}`); //This process is pid 12554
監聽事件
常用的事件有beforeExit、exit、uncaughtException、message
beforeExit與exit的區別有兩方面:
因此下面的代碼console都不會被執行
process.on('beforeExit', function(code) { console.log('before exit: '+ code);});process.on('exit', function(code) { setTimeout(function() { console.log('exit: ' + code); }, 0);});a.b();
當異常一直沒有被捕獲處理的話,最后就會觸發'uncaughtException'事件。默認情況下,Node.js會打印堆棧信息到stderr然后退出進程。不要試圖阻止uncaughtException退出進程,因此此時程序的狀態可能已經不穩定了,建議的方式是及時捕獲處理代碼中的錯誤,uncaughtException里面只做一些清理工作(可以執行異步代碼)。
注意:node的9.3版本增加了process.setUncaughtExceptionCaptureCallback方法
當process.setUncaughtExceptionCaptureCallback(fn)指定了監聽函數的時候,uncaughtException事件將會不再被觸發。
process.on('uncaughtException', function() { console.log('uncaught listener');});process.setUncaughtExceptionCaptureCallback(function() { console.log('uncaught fn');});a.b();// uncaught fn
message適用于父子進程之間發送消息,關于如何創建父子進程會放在child_process模塊中進行。
調度任務
process.nextTick(fn)
通過process.nextTick調度的任務是異步任務,EventLoop是分階段的,每個階段執行特定的任務,而nextTick的任務在階段切換的時候就會執行,因此nextTick會比setTimeout(fn, 0)更快的執行,關于EventLoop見下圖,后面會做進一步詳細的講解
發出警告
process.emitWarning('Something warning happened!', { code: 'MY_WARNING', type: 'XXXX'});// (node:14771) [MY_WARNING] XXXX: Something warning happened!
當type為DeprecationWarning時,可以通過命令行選項施加影響
--throw-deprecation
會拋出異常--no-deprecation
不輸出DeprecationWarning--trace-deprecation
打印詳細堆棧信息process.emitWarning('Something warning happened!', { type: 'DeprecationWarning'});console.log(4);node --throw-deprecation index.jsnode --no-deprecation index.jsnode --trace-deprecation index.js
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答