Electron 进程
官方文档:
https://www.electronjs.org/docs/api/process
官方访问有点儿慢,这里复制过来方便自己查阅
处理对象的扩展
Electron's process
对象继承 Node.js process
object。 它新增了以下事件、属性和方法
注意:
因为Electron's process
对象继承自node的 进程对象,所以如果在渲染进程中使用process
则需要在主线程中开启
//注入node模块
nodeIntegration: true
例:
function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
win.loadFile('index.html')
}
Sandbox
在沙盒化的渲染进程中, process
对象只包含了API的一个子集:
crash()
hang()
getCreationTime()
getHeapStatistics()
getBlinkMemoryInfo()
getProcessMemoryInfo()
getSystemMemoryInfo()
getSystemVersion()
getCPUUsage()
getIOCounters()
argv
execPath
env
pid
arch
platform
沙盒化
type
version
versions
mas
windowsStore
例:获取系统信息
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>
<body style="background: white;">
<h1>进程</h1>
<button onclick="getProcessUseInfo()">点击查看</button>
</body>
<script src="render.js"></script>
</html>
render.js
function getProcessUseInfo() {
console.log('getProcessUseage', process.getCPUUsage());
console.log('env', process.env);
}
效果
事件
EN
事件: 'loaded'
当Electron加载了它的内部初始化脚本并且是正要开始加载网页或主脚本时触发。
当node集成被关闭时,预加载脚本可以使用它将删除的 Node global symbols 添加回全局范围:
// preload.js
const _setImmediate = setImmediate
const _clearImmediate = clearImmediate
process.once('loaded', () => {
global.setImmediate = _setImmediate
global.clearImmediate = _clearImmediate
})
属性
process.defaultApp
Readonly
A Boolean
. When app is started by being passed as parameter to the default app, this property is true
in the main process, otherwise it is undefined
.
EN
process.isMainFrame
Readonly
A Boolean
, true
when the current renderer context is the "main" renderer frame. If you want the ID of the current frame you should use webFrame.routingId
.
EN
process.mas
Readonly
A Boolean
. For Mac App Store build, this property is true
, for other builds it is undefined
.
EN
process.noAsar
A Boolean
that controls ASAR support inside your application. Setting this to true
will disable the support for asar
archives in Node's built-in modules.
EN
process.noDeprecation
Boolean
类型,用于控制弃用警告是否被打印到stderr
。 将其设置为true
将会禁用弃用警告。 使用此属性代替 -no-deprecation
命令行标志。
EN
process.resourcesPath
Readonly
String
类型, 表示资源目录的路径。
EN
process.sandboxed
Readonly
A Boolean
. When the renderer process is sandboxed, this property is true
, otherwise it is undefined
.
EN
process.throwDeprecation
Boolean
类型,用于控制是否将弃用警告当做例外。 设置它为 true
时会抛出错误。 使用此属性代替 --throw-deprecation
命令行标志。
EN
process.traceDeprecation
Boolean
类型,用于控制打印到 stderr
的弃用中是否包含其堆栈跟踪。 将此设置为 true
将会打印对弃用的堆栈跟踪。 此属性代替 --trace-deprecation
命令行标志。
EN
process.traceProcessWarnings
一个 Boolean
, 用于控制是否将进程的警告打印到包含堆栈跟踪的 stderr
中 。 将此设置为 true
将打印对进程警告的堆栈跟踪(包括弃用)。 此属性代替 --trace-warnings
命令行标志。
EN
process.type
Readonly
A String
representing the current process's type, can be:
browser
- The main processrenderer
- A renderer processworker
- In a web worker
EN
process.versions.chrome
Readonly
string
,一个表示 Chrome 版本的字符串。
EN
process.versions.electron
Readonly
string
,一个表示 Electron 版本的字符串。
EN
process.windowsStore
Readonly
A Boolean
. If the app is running as a Windows Store app (appx), this property is true
, for otherwise it is undefined
.
EN
方法
process
对象具有以下方法:
EN
process.crash()
导致当前进程崩溃的主线程。
EN
process.getCreationTime()
返回 Number | null
-从纪元开始的毫秒数,如果信息不可用则返回null
Indicates the creation time of the application. The time is represented as number of milliseconds since epoch. It returns null if it is unable to get the process creation time.
EN
process.getCPUUsage()
返回 CPUUsage
EN
process.getIOCounters()
Windows**Linux
返回 IOCounters
EN
process.getHeapStatistics()
返回 Object
:
totalHeapSize
IntegertotalHeapSizeExecutable
IntegertotalPhysicalSize
IntegertotalAvailableSize
IntegerusedHeapSize
IntegerheapSizeLimit
IntegermallocedMemory
IntegerpeakMallocedMemory
IntegerdoesZapGarbage
Boolean
Returns an object with V8 heap statistics. 备注:所有数据值以KB为单位
EN
process.getBlinkMemoryInfo()
返回 Object
:
allocated
Integer - Size of all allocated objects in Kilobytes.marked
Integer - Size of all marked objects in Kilobytes.total
Integer - Total allocated space in Kilobytes.
Returns an object with Blink memory information. It can be useful for debugging rendering / DOM related memory issues. Note that all values are reported in Kilobytes.
EN
process.getProcessMemoryInfo()
Returns Promise<ProcessMemoryInfo>
- Resolves with a ProcessMemoryInfo
Returns an object giving memory usage statistics about the current process. Note that all statistics are reported in Kilobytes. This api should be called after app ready.
Chromium does not provide residentSet
value for macOS. This is because macOS performs in-memory compression of pages that haven't been recently used. As a result the resident set size value is not what one would expect. private
memory is more representative of the actual pre-compression memory usage of the process on macOS.
EN
process.getSystemMemoryInfo()
返回 Object
:
total
Integer - 系统可用的物理内存总量(Kb)。free
Integer - 应用程序或磁盘缓存未使用的内存总量。swapTotal
Integer Windows Linux - 系统交换内存容量(单位:千字节)。swapFree
Integer Windows Linux - 系统可用交换内存大小(单位:千字节)。
Returns an object giving memory usage statistics about the entire system. Note that all statistics are reported in Kilobytes.
EN
process.getSystemVersion()
Returns String
- The version of the host operating system.
示例:
const version = process.getSystemVersion()
console.log(version)
// On macOS -> '10.13.6'
// On Windows -> '10.0.17763'
// On Linux -> '4.15.0-45-generic'
复制
Note: It returns the actual operating system version instead of kernel version on macOS unlike os.release()
.
EN
process.takeHeapSnapshot(filePath)
filePath
String - Path to the output file.
Returns Boolean
- Indicates whether the snapshot has been created successfully.
Takes a V8 heap snapshot and saves it to filePath
.
EN
process.hang()
导致当前进程挂起的主线程。
EN
process.setFdLimit(maxDescriptors)
macOS**Linux
maxDescriptors
Integer
将文件描述符的软限制设置为 maxDescriptors
或 OS 硬限制, 其中以当前进程较低的值为准。