当前位置: 首页 > 文档资料 > Electron 中文文档 >

进程

优质
小牛编辑
119浏览
2023-12-01

处理对象的扩展

进程: Main, Renderer

Electron's process 对象继承 Node.js process object。 它新增了以下事件、属性和方法

process

Extensions to process object.

Process: Main, Renderer

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

Sandbox

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

  • crash()
  • hang()
  • getCreationTime()
  • getHeapStatistics()
  • getProcessMemoryInfo()
  • getSystemMemoryInfo()
  • getSystemVersion()
  • getCPUUsage()
  • getIOCounters()
  • argv
  • execPath
  • env
  • pid
  • arch
  • platform
  • 沙盒化
  • type
  • version
  • versions
  • mas
  • windowsStore

Sandbox

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

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

事件

Events

事件: 'loaded'

当Electron加载了它的内部初始化脚本并且是正要开始加载网页或主脚本时触发。

当node集成被关闭时,预加载脚本可以使用它将删除的 Node global symbols 添加回全局范围:

// preload.js
const _setImmediate = setImmediate
const _clearImmediate = clearImmediate
process.once('loaded', () => {
  global.setImmediate = _setImmediate
  global.clearImmediate = _clearImmediate
})

Event: 'loaded'

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

It can be used by the preload script to add removed Node global symbols back to the global scope when node integration is turned off:

// preload.js
const _setImmediate = setImmediate
const _clearImmediate = clearImmediate
process.once('loaded', () => {
  global.setImmediate = _setImmediate
  global.clearImmediate = _clearImmediate
})

属性

Properties

process.defaultApp

Boolean类型, 当作为参数传递给默认应用程序启动应用时,该属性在主进程中为true,否则为undefined

process.defaultApp

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.

process.isMainFrame

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

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

Boolean类型,为 Mac App Store 生成, 此属性为 true, 对于其他生成,则为 undefined

process.mas

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

process.noAsar

Boolean类型, 用于控制应用程序内的 ASAR 支持。将此设置为 true 将在Node的内置模块中禁用对 asar 的支持。

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.

process.noDeprecation

Boolean 类型,用于控制弃用警告是否被打印到stderr。 将其设置为true将会禁用弃用警告。 使用此属性代替 -no-deprecation 命令行标志。

process.noDeprecation

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

A Boolean that controls whether or not deprecation warnings are printed to stderr when formerly callback-based APIs converted to Promises are invoked using callbacks. Setting this to true will enable deprecation warnings.

process.enablePromiseAPIs

A Boolean that controls whether or not deprecation warnings are printed to stderr when formerly callback-based APIs converted to Promises are invoked using callbacks. Setting this to true will enable deprecation warnings.

process.resourcesPath

String 类型, 表示资源目录的路径。

process.resourcesPath

A String representing the path to the resources directory.

process.sandboxed

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

process.sandboxed

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

process.throwDeprecation

Boolean类型,用于控制是否将弃用警告当做例外。 设置它为 true 时会抛出错误。 使用此属性代替 --throw-deprecation 命令行标志。

process.throwDeprecation

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 命令行标志。

process.traceDeprecation

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 命令行标志。

process.traceProcessWarnings

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

A String representing the current process's type, can be "browser" (i.e. main process), "renderer", or "worker" (i.e. web worker).

process.type

A String representing the current process's type, can be "browser" (i.e. main process), "renderer", or "worker" (i.e. web worker).

process.versions.chrome

string,一个表示 Chrome 版本的字符串。

process.versions.chrome

A String representing Chrome's version string.

process.versions.electron

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

process.versions.electron

A String representing Electron's version string.

process.windowsStore

Boolean. 如果应用运行于 Windows 商店(appx), 改属性为true, 否则为 undefined.

process.windowsStore

A Boolean. If the app is running as a Windows Store app (appx), this property is true, for otherwise it is undefined.

方法

process 对象具有以下方法:

Methods

The process object has the following methods:

process.crash()

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

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

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.getCreationTime()

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

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

process.getCPUUsage()

Returns CPUUsage

process.getIOCounters() Windows Linux

返回 IOCounters

process.getIOCounters() Windows Linux

Returns IOCounters

process.getHeapStatistics()

返回 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. Note that all statistics are reported in Kilobytes.

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. Note that all statistics 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.

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

process.getSystemMemoryInfo()

返回 Object:

  • total Integer - 系统可用的物理内存总量(Kb)。
  • free Integer - 应用程序或磁盘缓存未使用的内存总量。
  • swapTotal Integer Windows Linux - 系统交换内存容量(单位:千字节)。
  • swapFree Integer Windows Linux - 系统可用交换内存大小(单位:千字节)。

返回一个对象, 它提供有关当前进程的内存使用情况统计信息。请注意, 所有统计信息都以千字节为单位报告。

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.

示例

平台Version
macOS10.13.6
Windows10.0.17763
Linux4.15.0-45-generic

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

process.getSystemVersion()

Returns String - The version of the host operating system.

Examples:

PlatformVersion
macOS10.13.6
Windows10.0.17763
Linux4.15.0-45-generic

Note: It returns the actual operating system version instead of kernel version on macOS unlike 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.

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.

process.hang()

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

process.hang()

Causes the main thread of the current process hang.

process.setFdLimit(maxDescriptors) macOS Linux

  • maxDescriptors Integer

将文件描述符的软限制设置为 maxDescriptors 或 OS 硬限制, 其中以当前进程较低的值为准。

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.