Skip to main content

process

流程对象的扩展。

¥Extensions to process object.

进程:主进程渲染器

¥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

事件:'loaded'

¥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

当当前渲染器上下文是 "main" 渲染器帧时为 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 App Store 版本,此属性为 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

控制是否将弃用警告打印到 stderrboolean。将其设置为 true 将消除弃用警告。使用此属性代替 --no-deprecation 命令行标志。

¥A boolean 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 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 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 - 主进程

    ¥browser - The main process

  • renderer - 渲染器进程

    ¥renderer - A renderer process

  • worker - 在网络工作者中

    ¥worker - In a web worker

  • utility - 在作为服务启动的节点进程中

    ¥utility - In a node process launched as a service

process.versions.chrome 只读

¥process.versions.chrome Readonly

string 代表 Chrome 的版本字符串。

¥A string representing Chrome's version string.

process.versions.electron 只读

¥process.versions.electron Readonly

string 代表 Electron 的版本字符串。

¥A string representing Electron's version string.

process.windowsStore 只读

¥process.windowsStore Readonly

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

¥A boolean. If the app is running as a Windows Store app (appx), this property is true, for 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(或 null),则为 Electron.ParentPort 属性。

¥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 整数

    ¥totalHeapSize Integer

  • totalHeapSizeExecutable 整数

    ¥totalHeapSizeExecutable Integer

  • totalPhysicalSize 整数

    ¥totalPhysicalSize Integer

  • totalAvailableSize 整数

    ¥totalAvailableSize Integer

  • usedHeapSize 整数

    ¥usedHeapSize Integer

  • heapSizeLimit 整数

    ¥heapSizeLimit Integer

  • mallocedMemory 整数

    ¥mallocedMemory Integer

  • peakMallocedMemory 整数

    ¥peakMallocedMemory Integer

  • doesZapGarbage 布尔值

    ¥doesZapGarbage boolean

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

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

process.getBlinkMemoryInfo()

返回 Object

¥Returns Object:

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

    ¥allocated Integer - Size of all allocated objects in Kilobytes.

  • total 整数 - 分配的总空间(以千字节为单位)。

    ¥total Integer - Total allocated space in Kilobytes.

返回一个带有 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> - 用 进程内存信息 解决

¥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 对最近未使用的页面执行内存压缩。因此,常驻设定大小值并不是人们所期望的。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 整数 - 系统可用的物理内存总量(以千字节为单位)。

    ¥total Integer - The total amount of physical memory in Kilobytes available to the system.

  • free 整数 - 应用或磁盘缓存未使用的内存总量。

    ¥free Integer - The total amount of memory not being used by applications or disk cache.

  • swapTotal 整数 Windows Linux - 系统可用的交换内存总量(以千字节为单位)。

    ¥swapTotal Integer Windows Linux - The total amount of swap memory in Kilobytes available to the system.

  • swapFree 整数 Windows Linux - 系统可用的可用交换内存量(以千字节为单位)。

    ¥swapFree Integer Windows Linux - The free amount of swap memory in Kilobytes available to the system.

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

¥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'

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

¥Note: It returns the actual operating system version instead of kernel version on macOS unlike os.release().

process.takeHeapSnapshot(filePath)

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

    ¥filePath string - Path to the output file.

返回 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 Integer

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

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