harmony 鸿蒙@ohos.app.ability.childProcessManager (childProcessManager)

  • 2023-10-30
  • 浏览 (213)

@ohos.app.ability.childProcessManager (childProcessManager)

childProcessManager模块提供子进程管理能力,支持子进程启动操作。

说明:

本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

本模块接口仅可在Stage模型下使用。

导入模块

import childProcessManager from '@ohos.app.ability.childProcessManager';

childProcessManager.StartMode

子进程启动模式枚举。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

名称 说明
SELF_FORK 0 从App自身进程Fork子进程。以该模式启动的子进程中不能进行Binder IPC调用,会导致子进程Crash。

childProcessManager.startChildProcess

startChildProcess(srcEntry: string, startMode: StartMode): Promise<number>;

启动子进程,并调用子进程的入口方法,以Promise方式返回执行结果。创建子进程成功会返回子进程pid,但并不代表入口方法调用成功,具体结果以入口方法是否调用成功为准。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

参数:

参数名 类型 必填 说明
srcEntry string 子进程源文件相对路径。
startMode StartMode 子进程启动模式。

返回值:

类型 说明
Promise<number> 以Promise的形式返回子进程pid。

错误码

以下错误码详细介绍请参考errcode-ability

错误码ID 错误信息
16000050 Internal error.
16000061 Operation not supported.
16000062 The number of child process exceeds upper bound.

示例:

// 在entry模块的src/main/ets/process下创建DemoProcess.ts子进程类:
import ChildProcess from '@ohos.app.ability.ChildProcess';

export default class DemoProcess extends ChildProcess {
  onStart() {
    console.log("DemoProcess OnStart() called");
  }
}

// 使用childProcessManager.startChildProcess方法启动子进程:
import childProcessManager from '@ohos.app.ability.childProcessManager';

try {
  childProcessManager.startChildProcess("./ets/process/DemoProcess.ts", childProcessManager.StartMode.SELF_FORK)
    .then((data) => {
      console.log(`startChildProcess success, pid: ${data}`);
    }, (err: BusinessError) => {
      console.error(`startChildProcess error, errorCode: ${err.code}`);
    })
} catch (err) {
  console.error(`startChildProcess error, errorCode: ${err.code}`);
}

childProcessManager.startChildProcess

startChildProcess(srcEntry: string, startMode: StartMode, callback: AsyncCallback<number>): void;

启动子进程,并调用子进程的入口方法,以Callback方式返回执行结果。创建子进程成功会返回子进程pid,但并不代表入口方法调用成功,具体结果以入口方法是否调用成功为准。

系统能力:SystemCapability.Ability.AbilityRuntime.Core

参数:

参数名 类型 必填 说明
srcEntry string 子进程源文件相对路径。
startMode StartMode 子进程启动模式。
callback AsyncCallback<number> 以callback的形式返回子进程pid。

错误码

以下错误码详细介绍请参考errcode-ability

错误码ID 错误信息
16000050 Internal error.
16000061 Operation not supported.
16000062 The number of child process exceeds upper bound.

示例:

// 在entry模块的src/main/ets/process下创建DemoProcess.ts子进程类:
import ChildProcess from '@ohos.app.ability.ChildProcess';

export default class DemoProcess extends ChildProcess {
  onStart() {
    console.log("DemoProcess OnStart() called");
  }
}

// 使用childProcessManager.startChildProcess方法启动子进程:
import childProcessManager from '@ohos.app.ability.childProcessManager';

try {
  childProcessManager.startChildProcess("./ets/process/DemoProcess.ts", childProcessManager.StartMode.SELF_FORK, (err, data) => {
    if (data) {
      console.log(`startChildProcess success, pid: ${data}`);
    } else {
      console.error(`startChildProcess error, errorCode: ${err.code}`);
    }
  });
} catch (err) {
  console.error(`startChildProcess error, errorCode: ${err.code}`);
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙接口

harmony 鸿蒙系统公共事件定义(待停用)

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

harmony 鸿蒙企业设备管理概述(仅对系统应用开放)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager模块)

harmony 鸿蒙@ohos.distributedBundle (分布式包管理)

harmony 鸿蒙@ohos.bundle (Bundle模块)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (企业设备管理扩展能力)

0  赞