Skip to main content

process

处理对象的扩展。

进程:主进程渲染器

🌐 Process: Main, Renderer

Electron 的 process 对象是从 Node.js 的 process 对象 扩展而来的。它增加了以下事件、属性和方法:

🌐 Electron's process object is extended from the Node.js process object. It adds the following events, properties, and methods:

沙盒

🌐 Sandbox

在受沙箱保护的渲染器中,process 对象仅包含部分 API:

🌐 In sandboxed renderers the process object contains only a subset of the APIs:

  • crash()
  • hang()
  • getCreationTime()
  • getHeapStatistics()
  • getBlinkMemoryInfo()
  • getProcessMemoryInfo()
  • getSystemMemoryInfo()
  • getSystemVersion()
  • getCPUUsage()
  • uptime()
  • argv
  • execPath
  • env
  • pid
  • arch
  • platform
  • sandboxed
  • contextIsolated
  • type
  • version
  • versions
  • mas
  • windowsStore
  • contextId

事件

🌐 Events

Event: 'loaded'

当 Electron 加载其内部初始化脚本并开始加载网页或主脚本时触发。

🌐 Emitted when Electron has loaded its internal initialization script and is beginning to load the web page or the main script.

属性

🌐 Properties

process.defaultApp 只读

🌐 process.defaultApp Readonly

一个 boolean。当应用被作为参数传递给默认的 Electron 可执行文件启动时,这个属性在主进程中是 true,否则它是 undefined。 例如,当使用 electron . 运行应用时,它是 true,即使应用已打包(isPackaged)是 true。 这对于确定需要从 process.argv` 中切掉多少个参数非常有用。

🌐 A 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 只读

🌐 process.isMainFrame Readonly

在当前渲染器上下文是“主”渲染器帧时,booleantrue。如果你想获取当前帧的 ID,应使用 webFrame.routingId

🌐 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.

process.mas 只读

🌐 process.mas Readonly

一个 boolean。对于 Mac 应用商店版本,此属性为 true,对于其他版本则为 undefined

🌐 A boolean. For Mac App Store build, this property is true, for other builds it is undefined.

process.noAsar

一个用于在应用中控制 ASAR 支持的 boolean。将其设置为 true 将禁用 Node 内置模块中对 asar 压缩包的支持。

🌐 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.

process.noDeprecation

一个 boolean(可选)用于控制是否将弃用警告打印到 stderr。将其设置为 true 将会屏蔽弃用警告。此属性用于替代 --no-deprecation 命令行标志。

🌐 A boolean (optional) that controls whether or not deprecation warnings are printed to stderr. Setting this to true will silence deprecation warnings. This property is used instead of the --no-deprecation command line flag.

process.resourcesPath 只读

🌐 process.resourcesPath Readonly

string 表示资源目录的路径。

🌐 A string representing the path to the resources directory.

process.sandboxed 只读

🌐 process.sandboxed Readonly

一个 boolean。当渲染进程被沙箱化时,此属性为 true,否则为 undefined

🌐 A boolean. When the renderer process is sandboxed, this property is true, otherwise it is undefined.

process.contextIsolated 只读

🌐 process.contextIsolated Readonly

一个 boolean,用于指示当前渲染器上下文是否启用了 contextIsolation。在主进程中它是 undefined

🌐 A boolean that indicates whether the current renderer context has contextIsolation enabled. It is undefined in the main process.

process.throwDeprecation

boolean 用于控制是否将弃用警告作为异常抛出。将其设置为 true 将会对弃用情况抛出错误。此属性使用于替代 --throw-deprecation 命令行标志。

🌐 A boolean that controls whether or not deprecation warnings will be thrown as exceptions. Setting this to true will throw errors for deprecations. This property is used instead of the --throw-deprecation command line flag.

process.traceDeprecation

一个 boolean,用于控制打印到 stderr 的弃用信息是否包含其堆栈跟踪。将其设置为 true 将为弃用信息打印堆栈跟踪。此属性用来替代 --trace-deprecation 命令行标志。

🌐 A boolean that controls whether or not deprecations printed to stderr include their stack trace. Setting this to true will print stack traces for deprecations. This property is used instead of the --trace-deprecation command line flag.

process.traceProcessWarnings

boolean 用于控制打印到 stderr 的进程警告是否包含其堆栈跟踪。将其设置为 true 将打印进程警告的堆栈跟踪(包括弃用警告)。此属性用于替代 --trace-warnings 命令行标志。

🌐 A boolean that controls whether or not process warnings printed to stderr include their stack trace. Setting this to true will print stack traces for process warnings (including deprecations). This property is used instead of the --trace-warnings command line flag.

process.type 只读

🌐 process.type Readonly

string 表示当前进程的类型,可以是:

🌐 A string representing the current process's type, can be:

  • browser - 主进程
  • renderer - 渲染器进程
  • service-worker - 在服务工作者中
  • worker - 在网络工作者中
  • utility - 在作为服务启动的节点进程中

process.versions.chrome 只读

🌐 process.versions.chrome Readonly

string 表示 Chrome 的版本字符串。

🌐 A string representing Chrome's version string.

process.versions.electron 只读

🌐 process.versions.electron Readonly

一个表示 Electron 版本字符串的 string

🌐 A string representing Electron's version string.

process.windowsStore 只读

🌐 process.windowsStore Readonly

一个 boolean。如果应用作为 MSIX 包运行(包括 Windows 商店的 AppX),此属性为 true,否则为 undefined

🌐 A boolean. If the app is running as an MSIX package (including AppX for Windows Store), this property is true, otherwise it is undefined.

process.contextId 只读

🌐 process.contextId Readonly

string(可选)表示当前 JavaScript 上下文的全局唯一 ID。每个框架都有自己的 JavaScript 上下文。当启用 contextIsolation 时,隔离世界也有一个单独的 JavaScript 上下文。此属性仅在渲染进程中可用。

🌐 A string (optional) representing a globally unique ID of the current JavaScript context. Each frame has its own JavaScript context. When contextIsolation is enabled, the isolated world also has a separate JavaScript context. This property is only available in the renderer process.

process.parentPort

如果这是 UtilityProcess,则为 Electron.ParentPort 属性(否则为 null),允许与父进程通信。

🌐 A Electron.ParentPort property if this is a UtilityProcess (or null otherwise) allowing communication with the parent process.

方法

🌐 Methods

process 对象具有以下方法:

🌐 The process object has the following methods:

process.crash()

导致当前进程的主线程崩溃。

🌐 Causes the main thread of the current process crash.

process.getCreationTime()

返回 number | null - 自纪元以来的毫秒数,如果信息不可用则返回 null

🌐 Returns number | null - The number of milliseconds since epoch, or null if the information is unavailable

指示应用的创建时间。时间以自纪元以来的毫秒数表示。如果无法获取进程创建时间,则返回 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.

process.getCPUUsage()

返回 CPUUsage

🌐 Returns CPUUsage

process.getHeapStatistics()

返回 Object

🌐 Returns Object:

  • totalHeapSize 整数
  • totalHeapSizeExecutable 整数
  • totalPhysicalSize 整数
  • totalAvailableSize 整数
  • usedHeapSize 整数
  • heapSizeLimit 整数
  • mallocedMemory 整数
  • peakMallocedMemory 整数
  • doesZapGarbage 布尔值

返回一个包含 V8 堆统计信息的对象。请注意,所有统计数据均以千字节为单位报告。

🌐 Returns an object with V8 heap statistics. Note that all statistics are reported in Kilobytes.

process.getBlinkMemoryInfo()

返回 Object

🌐 Returns Object:

  • allocated 整数 - 所有分配对象的大小,以千字节为单位。
  • total 整数 - 分配的总空间(以千字节为单位)。

返回一个包含 Blink 内存信息的对象。它对于调试渲染/DOM 相关的内存问题很有用。请注意,所有数值均以千字节为单位报告。

🌐 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.

process.getProcessMemoryInfo()

返回 Promise<ProcessMemoryInfo> - 解析为 ProcessMemoryInfo

🌐 Returns Promise<ProcessMemoryInfo> - Resolves with a ProcessMemoryInfo

返回一个对象,提供有关当前进程的内存使用情况统计。请注意,所有统计数据均以千字节为单位。该 API 应在应用准备好后调用。

🌐 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 不为 macOS 提供 residentSet 值。这是因为 macOS 会对最近未使用的内存页进行内存压缩。因此,常驻集大小(RSS)值并不像预期的那样。private 内存更能反映该进程在 macOS 上压缩前的实际内存使用情况。

🌐 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.

process.getSystemMemoryInfo()

返回 Object

🌐 Returns Object:

  • total 整数 - 系统可用的物理内存总量,以千字节为单位。
  • free 整数 - 没有被应用或磁盘缓存使用的总内存量。
  • fileBacked 整数 macOS - 当前已分页到存储的内存量。 包括文件缓存、网络缓冲区和其他系统服务的内存。
  • purgeable 整数 macOS - 被标记为“可清理”的内存量。如果内存压力增加,系统可以回收这部分内存。
  • swapTotal 整数 Windows Linux - 系统可用的交换内存总量,单位为千字节。
  • swapFree 整数 Windows Linux - 系统可用的交换内存量(以千字节为单位)。

返回一个对象,提供有关整个系统的内存使用情况的统计信息。请注意,所有统计数据均以千字节为单位报告。

🌐 Returns an object giving memory usage statistics about the entire system. Note that all statistics are reported in Kilobytes.

process.getSystemVersion()

返回 string - 主机操作系统的版本。

🌐 Returns string - The version of the host operating system.

示例:

🌐 Example:

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

os.release() 不同,它返回的是 macOS 的实际操作系统版本,而不是内核版本。

process.takeHeapSnapshot(filePath)

  • filePath 字符串 - 输出文件的路径。

返回 boolean - 指示快照是否已成功创建。

🌐 Returns boolean - Indicates whether the snapshot has been created successfully.

获取 V8 堆快照并将其保存到 filePath

🌐 Takes a V8 heap snapshot and saves it to filePath.

process.hang()

导致当前进程的主线程挂起。

🌐 Causes the main thread of the current process hang.

process.setFdLimit(maxDescriptors) macOS Linux

  • maxDescriptors 整数

将文件描述符的软限制设置为 maxDescriptors 或操作系统硬限制,以较低者为准,针对当前进程。

🌐 Sets the file descriptor soft limit to maxDescriptors or the OS hard limit, whichever is lower for the current process.