process
处理对象的扩展
Electron's process 对象继承 Node.js process object。 它新增了以下事件、属性和方法
Sandbox
在沙盒化的渲染进程中, process 对象只包含了API的一个子集:
crash()hang()getCreationTime()getHeapStatistics()getBlinkMemoryInfo()getProcessMemoryInfo()getSystemMemoryInfo()getSystemVersion()getCPUUsage()getIOCounters()uptime()argvexecPathenvpidarchplatform沙盒化contextIsolatedtypeversionversionsmaswindowsStorecontextId
事件
事件: 'loaded'
当Electron加载了它的内部初始化脚本并且是正要开始加载网页或主脚本时触发。
属性
process.defaultApp 只读
一个 boolean。 When the app is started by being passed as parameter to the default Electron executable, this property is true in the main process, otherwise it is undefined. For example when running the app with electron ., it is true, even if the app is packaged (isPackaged) is true. This can be useful to determine how many arguments will need to be sliced off from process.argv.
process.isMainFrame 只读
boolean,若当前渲染器上下文是 渲染器"主"框架时为true。 如果你想得到当前框架的ID,你应该使用 webFrame.routingId。
process.mas 只读
一个 boolean。 为Mac App Store 构建,该属性是 true,为其他构建则为undefined。
process.noAsar
boolean 控制您应用程序内的 ASAR 支持。 设置为 true将会禁用Node内置模块中对 asar 的支持。
process.noDeprecation
boolean 类型,用于控制弃用警告是否被打印到stderr。 将其设置为true将会禁用弃用警告。 使用此属性代替 -no-deprecation 命令行标志。
process.resourcesPath 只读
一个 string 值,表示 resources 目录的路径。
process.sandboxed 只读
一个 boolean。 当渲染器进程被沙盒化时,该属性是 true,否则是 undefined。
process.contextIsolated 只读
一个boolean类型的值指明当前渲染上下文是否启用了contextIsolation。 这在主进程中是undefined的。
process.throwDeprecation
boolean类型,用于控制是否将弃用警告当做例外。 设置它为 true 时会抛出错误。 使用此属性代替 --throw-deprecation 命令行标志。
process.traceDeprecation
一个 boolean 值,用于控制是否打印到 stderr 中弃用信息,包含堆栈跟踪。 将此设置为 true 将会打印对弃用的堆栈跟踪。 此属性代替 --trace-deprecation 命令行标志。
process.traceProcessWarnings
一个 boolean 值,用于控制是否打印到 stderr 中进程警告信息,包括堆栈跟踪。 将此设置为 true 将打印对进程警告的堆栈跟踪(包括弃用)。 此属性代替 --trace-warnings 命令行标志。
process.type 只读
string 代表当前进程的类型,可以是:
browser- 主进程renderer- 渲染进程worker- web workerutility- 运行在 service 中的 node 进程。
process.versions.chrome 只读
一个 string 值,表示 Chrome 版本号。
process.versions.electron 只读
一个 string 值,表示 Electron 版本号。
process.windowsStore 只读
一个 boolean。 如果应用以 Windows 商店应用(appx) 运行,该属性为true,否则为 undefined。
process.contextId 只读
一个 string 值(可选) 表示当前 JavaScript 上下文的全局唯一 ID。 每个对话框都有自己的 JavaScript 上下文。 当上下文隔离被启用,被隔离的环境中也有单独的JavaScript上下文。 该属性仅在渲染进程中可用。
process.parentPort
Electron.ParentPort 属性,如果这是一个 UtilityProcess (或否则为 null) 允许与父进程通信。
方法
process 对象具有以下方法:
process.crash()
导致当前进程崩溃的主线程。
process.getCreationTime()
返回 number | null -从纪元开始的毫秒数,如果信息不可用则返回null
指示应用程序的创建时间。 新时代(1970-01-01 00:00:00 UTC)以来的毫秒数表示的时间。 如果无法获得进程创建时间,则返回为空。
process.getCPUUsage()
返回 CPUUsage
process.getIOCounters() Windows Linux
返回 IOCounters
process.getHeapStatistics()
返回 Object:
totalHeapSizeIntegertotalHeapSizeExecutableIntegertotalPhysicalSizeIntegertotalAvailableSizeIntegerusedHeapSizeIntegerheapSizeLimitIntegermallocedMemoryIntegerpeakMallocedMemoryIntegerdoesZapGarbageboolean
返回包含 V8 堆统计的对象。 备注:所有数据值以KB为单位
process.getBlinkMemoryInfo()
返回 Object:
allocatedInteger - 所有已分配对象的大小(kb)totalInteger - 分配的总空间(kb)
返回带有Blink内存信息的对象。 可以用于调试渲染/DOM相关内存问题。 请注意,所有值都以KB为单位
process.getProcessMemoryInfo()
返回 Promise<ProcessMemoryInfo> - Promise成功返回 PrecessMemoryInfo
返回一个对象,提供当前进程的内存使用统计。 请注意,所有统计值都以KB为单位 这个api应该在应用程序准备就绪后被调用。
Chromium 没有为macOS提供 residentSet 值。 因为macOS对最近未使用过的页面进行内存压缩。 结果是原始设置大小的值不是我们所期望的。 私有 内存更能代表在 macOS 上进程的实际预压缩内存的使用情况。
process.getSystemMemoryInfo()
返回 Object:
totalInteger - 系统可用的物理内存总量(Kb)。freeInteger - 应用程序或磁盘缓存未使用的内存总量。swapTotalInteger Windows Linux - 系统交换内存容量(单位:千字节)。swapFreeInteger Windows Linux - 系统可用交换内存大小(单位:千字节)。
返回一个对象,提供整个系统的内存使用统计。 请注意,所有统计值都以KB为单位
process.getSystemVersion()
返回 string - 操作系统的版本。
示例:
const version = process.getSystemVersion()
console.log(version)
// On macOS -> '10.13.6'
// On Windows -> '10.0.17763'
// On Linux -> '4.15.0-45-generic'
注意: 它返回实际操作系统版本,而不是在 macOS 上的内核版本,不同于 os.release()。
process.takeHeapSnapshot(filePath)
filePathstring - 输出文件路径
返回 boolean - 指明快捷方式是否被成功创建。
获取V8堆快照并保存到 filePath
process.hang()
导致当前进程挂起的主线程。
process.setFdLimit(maxDescriptors) macOS Linux
maxDescriptorsInteger
将文件描述符的软限制设置为 maxDescriptors 或 OS 硬限制, 其中以当前进程较低的值为准。