OpenHarmony开发者论坛

标题: OpenHarmony应用开发时同时启动多个不同的worker出问题 [打印本页]

作者: linyu    时间: 2024-7-26 19:34
标题: OpenHarmony应用开发时同时启动多个不同的worker出问题
【问题描述】
问题现象和发生的背景
我定义了两种不同的worker——TimeWorker和RaftWorker,RaftWorker需要向RaftWorker请求计时结果,同时UI线程需要向RaftWorker发送数据请求,进行一致性处理,但是在运行时出现bug。

相关的代码,截图,日志信息
RaftWorker.ets的代码:
import worker, { ThreadWorkerGlobalScope, MessageEvents, ErrorEvent } from '@ohos.worker';
import distributedKVStore from '@ohos.data.distributedKVStore';
import { GlobalContext } from '@ohos/dataorm';
import { raftStateMachine, Operation } from '../raft/RaftStateMachine';

const workerPort: ThreadWorkerGlobalScope = worker.workerPort;

const kvStore: distributedKVStore.SingleKVStore = GlobalContext.getContext().getValue('kVStore') as distributedKVStore.SingleKVStore

const applyLog = (op: Operation): void => {
  kvStore.put(op.key, op.val)
}

export const raftServer = new raftStateMachine(applyLog)

// const raftStateMachine: raftStateMachine = new raftStateMachine(applyLog)

/**
* Defines the event handler to be called when the worker thread receives a message sent by the host thread.
* The event handler is executed in the worker thread.
*
* @param e message data
*/
workerPort.onmessage = (e: MessageEvents): void => {
  raftServer.serve()
}

/**
* Defines the event handler to be called when the worker receives a message that cannot be deserialized.
* The event handler is executed in the worker thread.
*
* @param e message data
*/
workerPort.onmessageerror = (e: MessageEvents): void => {
}

/**
* Defines the event handler to be called when an exception occurs during worker execution.
* The event handler is executed in the worker thread.
*
* @param e error message
*/
workerPort.onerror = (e: ErrorEvent): void => {
}


TimeWorker.ts的代码如下:
import worker, { ThreadWorkerGlobalScope, MessageEvents, ErrorEvent } from '@ohos.worker';
import { CANCEL_TIMER, TIMER_NORMAL_END } from '../utils/ConstData';

const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
let isTimerSet: boolean = false
/**
* Defines the event handler to be called when the worker thread receives a message sent by the host thread.
* The event handler is executed in the worker thread.
*
* @param e message data
*/
// 接受主线程发来的计时请求,开始启动选举计时器
workerPort.onmessage = function (e: MessageEvents) {
  let data: number = e.data
  if (data == CANCEL_TIMER) {
    isTimerSet = false
} else {
    isTimerSet = true
setTimeout(()=>{
      console.log(data + " ms timeout");
    }, data)
    if (isTimerSet) {
      workerPort.postMessage(TIMER_NORMAL_END)
    }
  }
}

/**
* Defines the event handler to be called when the worker receives a message that cannot be deserialized.
* The event handler is executed in the worker thread.
*
* @param e message data
*/
workerPort.onmessageerror = function (e: MessageEvents) {
  console.log(e.data)
}

/**
* Defines the event handler to be called when an exception occurs during worker execution.
* The event handler is executed in the worker thread.
*
* @param e error message
*/
workerPort.onerror = function (e: ErrorEvent) {
  console.log(e.message)
}

遇到的问题报错如图:
Device info:OpenHarmony 3.2
Build info:OpenHarmony 4.1.7.7
Fingerprint:e8764f46a043ec63642bc29d819b819e9e65367a3f62f3b62e775d480e17eff1
Module name:com.serise.myapplication
Version:1.0.0
VersionCode:1000000
PreInstalled:No
Foreground:Yes
Pid:2715
Uid:20010044
Reason:TypeError
Error name:TypeError
Error message:Obj is not a Valid object
SourceCode:
workerPort.onmessage = (e: MessageEvents): void => {
^
Stacktrace:
    at func_main_0 (entry/src/main/ets/workers/RaftWorker.ets:24:1)


我尝试过的解决方法和结果
暂时没有解决的思路,也没找到相关的代码示例

我想要达到的结果
我要同时能够启动两个不同的worker线程(分别运行不同的代码逻辑),后面还可能会启动更多的worker

【运行环境】
硬件:Dayu200
DevEvoStudio版本:
DevEco Studio 4.0 Release
Build Version: 4.0.0.600, built on October 17, 2023
Build #DS-223.8617.56.36.400600
Runtime version: 17.0.6+10-b829.5 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 11 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 1536M
Cores: 12
Registry:
    external.system.auto.import.disabled=true
SDK版本:api10

作者: fengyunrenwu    时间: 2024-8-8 10:13
代码看起来可能有问题,方便附上最小能复现问题的demo么? 有demo才好判断。




欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/) Powered by Discuz! X3.5