Skip to main content

process

Extensions to process object.处理对象的扩展。

Process:进程:Main, Renderer

Electron's process object is extended from the Node.js process object. Electron的process对象是从Node.js process对象扩展而来的。It adds the following events, properties, and methods:它添加了以下事件、属性和方法:

Sandbox

In sandboxed renderers the process object contains only a subset of the APIs:在沙盒渲染器中,process对象只包含API的一个子集:

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

Events事件

Event: 'loaded'

Emitted when Electron has loaded its internal initialization script and is beginning to load the web page or the main script.当Electron加载其内部初始化脚本并开始加载网页或主脚本时发出。

Properties属性

process.defaultApp Readonly

A boolean. 一个booleanWhen app is started by being passed as parameter to the default app, this property is true in the main process, otherwise it is undefined.当应用程序作为参数传递给默认应用程序时,此属性在主进程中为true,否则为undefined

process.isMainFrame Readonly

A boolean, true when the current renderer context is the "main" renderer frame. 一个boolean,当当前渲染器上下文为“主”渲染器帧时为trueIf you want the ID of the current frame you should use webFrame.routingId.如果您想要当前帧的ID,您应该使用webFrame.routingId

process.mas Readonly

A boolean. 一个booleanFor Mac App Store build, this property is true, for other builds it is undefined.对于Mac App Store版本,此属性为true,而对于其他版本,则为undefined

process.noAsar

A boolean that controls ASAR support inside your application. 一个boolean,用于控制应用程序内部的ASAR支持。Setting this to true will disable the support for asar archives in Node's built-in modules.将此设置为true将禁用Node内置模块中对asar存档的支持。

process.noDeprecation

A boolean that controls whether or not deprecation warnings are printed to stderr. 一个boolean,用于控制是否将弃用警告打印到stderrSetting this to true will silence deprecation warnings. 将此设置为true将使弃用警告静音。This property is used instead of the --no-deprecation command line flag.使用此属性代替--no-deprecation命令行标志。

process.resourcesPath Readonly

A string representing the path to the resources directory.一个string,表示资源目录的路径。

process.sandboxed Readonly

A boolean. 一个boolean值。When the renderer process is sandboxed, this property is true, otherwise it is undefined.当渲染器进程被沙盒化时,此属性为true,否则为undefined

process.contextIsolated Readonly

A boolean that indicates whether the current renderer context has contextIsolation enabled. 一个boolean,指示当前渲染器上下文是否启用了contextIsolationIt is undefined in the main process.它在主进程中为undefined

process.throwDeprecation

A boolean that controls whether or not deprecation warnings will be thrown as exceptions. 一个boolean,用于控制是否将弃用警告作为异常抛出。Setting this to true will throw errors for deprecations. 将此设置为true将引发用于弃用的错误。This property is used instead of the --throw-deprecation command line flag.使用此属性而不是--throw-deprecation命令行标志。

process.traceDeprecation

A boolean that controls whether or not deprecations printed to stderr include their stack trace. 一个boolean,用于控制打印到stderr的弃用是否包括其堆栈跟踪。Setting this to true will print stack traces for deprecations. 将此设置为true将打印用于弃用的堆栈跟踪。This property is instead of the --trace-deprecation command line flag.此属性不是--trace-deprecation命令行标志。

process.traceProcessWarnings

A boolean that controls whether or not process warnings printed to stderr include their stack trace. 一个boolean,用于控制打印到stderr的进程警告是否包括其堆栈跟踪。Setting this to true will print stack traces for process warnings (including deprecations). 将此设置为true将打印进程警告(包括弃用)的堆栈跟踪。This property is instead of the --trace-warnings command line flag.此属性不是--trace-warnings命令行标志。

process.type Readonly

A string representing the current process's type, can be:表示当前进程类型的string,可以是:

  • browser - The main process主要流程
  • renderer - A renderer process渲染器进程
  • worker - In a web worker在网络工作者中

process.versions.chrome Readonly

A string representing Chrome's version string.表示Chrome版本字符串的string

process.versions.electron Readonly

A string representing Electron's version string.表示Electron版本字符串的string

process.windowsStore Readonly

A boolean. 一个booleanIf the app is running as a Windows Store app (appx), this property is true, for otherwise it is undefined.如果应用程序作为Windows应用商店应用程序(appx)运行,则此属性为true,否则为undefined

process.contextId Readonly

A string (optional) representing a globally unique ID of the current JavaScript context. 一个string(可选),表示当前JavaScript上下文的全局唯一ID。Each frame has its own JavaScript context. 每个帧都有自己的JavaScript上下文。When contextIsolation is enabled, the isolated world also has a separate JavaScript context. 当启用contextIsolation时,隔离的世界也有一个单独的JavaScript上下文。This property is only available in the renderer process.此属性仅在渲染器进程中可用。

Methods方法

The process object has the following methods:process对象具有以下方法:

process.crash()

Causes the main thread of the current process crash.导致当前进程的主线程崩溃。

process.getCreationTime()

Returns返回number | null - The number of milliseconds since epoch, or null if the information is unavailable自epoch以来的毫秒数,如果信息不可用,则为null

Indicates the creation time of the application. 指示应用程序的创建时间。The time is represented as number of milliseconds since epoch. 时间表示为自epoch以来的毫秒数。It returns null if it is unable to get the process creation time.如果无法获取进程创建时间,则返回null

process.getCPUUsage()

Returns 返回CPUUsage

process.getIOCounters() Windows Linux

Returns 返回IOCounters

process.getHeapStatistics()

Returns返回Object:

  • totalHeapSize Integer
  • totalHeapSizeExecutable Integer
  • totalPhysicalSize Integer
  • totalAvailableSize Integer
  • usedHeapSize Integer
  • heapSizeLimit Integer
  • mallocedMemory Integer
  • peakMallocedMemory Integer
  • doesZapGarbage boolean

Returns an object with V8 heap statistics. 返回一个具有V8堆统计信息的对象。Note that all statistics are reported in Kilobytes.请注意,所有统计数据都以千字节为单位报告。

process.getBlinkMemoryInfo()

Returns返回Object:

  • allocated Integer - Size of all allocated objects in Kilobytes.所有已分配对象的大小(以千字节为单位)。
  • total Integer - Total allocated space in Kilobytes.已分配的总空间(KB)。

Returns an object with Blink memory information. 返回具有闪烁内存信息的对象。It can be useful for debugging rendering / DOM related memory issues. 它对于调试渲染/DOM相关的内存问题非常有用。Note that all values are reported in Kilobytes.请注意,所有值都以千字节为单位报告。

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.这个api应该在应用程序准备好之后调用。

Chromium does not provide residentSet value for macOS. Chromium不为macOS提供residentSet值。This is because macOS performs in-memory compression of pages that haven't been recently used. 这是因为macOS对最近没有使用过的页面执行内存压缩。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.private内存更能代表macOS上进程的实际预压缩内存使用情况。

process.getSystemMemoryInfo()

Returns返回Object:

  • total Integer - The total amount of physical memory in Kilobytes available to the system.系统可用的物理内存总量(以千字节为单位)。
  • free Integer - The total amount of memory not being used by applications or disk cache.应用程序或磁盘缓存未使用的内存总量。
  • swapTotal Integer Windows Linux - The total amount of swap memory in Kilobytes available to the system.系统可用的交换内存总量(以千字节为单位)。
  • 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()

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: It returns the actual operating system version instead of kernel version on macOS unlike os.release().它返回实际的操作系统版本,而不是macOS上的内核版本,这与os.release()不同。

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.获取一个V8堆快照并将其保存到filePath中。

process.hang()

Causes the main thread of the current process hang.导致当前进程的主线程挂起。

process.setFdLimit(maxDescriptors) macOS Linux

  • maxDescriptors Integer

Sets the file descriptor soft limit to maxDescriptors or the OS hard limit, whichever is lower for the current process.将文件描述符软限制设置为maxDescriptors或OS硬限制,以当前进程的较低值为准。