您好,欢迎访问一九零五行业门户网

Node.js笔记process模块使用详解

这次给大家带来node.js笔记process模块使用详解,node.js笔记process模块使用的注意事项有哪些,下面就是实战案例,一起来看一下。
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); // dev node_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.js console.log(`module a: ${process.mainmodule === module}`); //b.js require('./a'); console.log(`module b: ${process.mainmodule === module}`); node b.js // 输出 module a: false module b: true
pid信息
console.log(`this process is pid ${process.pid}`); //this process is pid 12554
监听事件
常用的事件有beforeexit、exit、uncaughtexception、message
beforeexit与exit的区别有两方面:
beforeexit里面可以执行异步代码、exit只能是同步代
码手动调用process.exit()或者触发uncaptexception导致进程退出不会触发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.js node --no-deprecation index.js node --trace-deprecation index.js
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
怎么使用webpack3.0配置webpack-dev-server
js反射与依赖注入使用案例分析
以上就是node.js笔记process模块使用详解的详细内容。
其它类似信息

推荐信息