HarmonyOS 如何使用TaskPool进行异步IO,同时保证文件写入是顺序的

我们的场景是使用TaskPool进行异步IO,同时保证文件写入是顺序的,在上一个ArrayBuffer写完之后再去写下一个

HarmonyOS
2024-08-28 14:14:46
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

可以参考如下代码

import taskpool from '@ohos.taskpool'; 
import { DataUtil } from './DataUtil' 
import fs from '@ohos.file.fs'; 
 
@Concurrent 
function writeBuf(buf: ArrayBuffer, offset: number, filesDir: string) { 
  // 步骤1: 具体的图像处理操作及其他耗时操作 
  let fileName = 'test' 
  let filePath = `${filesDir}/${fileName}` 
  console.info(`Demo start:${offset}`) 
  // 新建并打开文件 
  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 
  fs.writeSync(file.fd, buf, { offset }); 
  // 关闭文件 
  fs.closeSync(file); 
  console.info(`Demo end:${offset}`) 
} 
 
function histogramStatistic(folderPath: string): void { 
  let group: taskpool.TaskGroup = new taskpool.TaskGroup(); 
  for (let i = 0; i < 3; i++) { 
    let buf = DataUtil.fakeDataArray() 
    group.addTask(writeBuf, buf, i * buf.byteLength, folderPath); 
  } 
  taskpool.execute(group, taskpool.Priority.HIGH).then((ret: Object) => { 
    console.info(`Demo taskPool task execute success`) 
  }) 
} 
 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World' 
 
  build() { 
    Row() { 
      Column({ space: 20 }) { 
        Button('WriteBuf') 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            const filesDir: string = getContext(this).tempDir 
            histogramStatistic(filesDir); 
          }) 
        Button('ReadBuf') 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            const filesDir: string = getContext(this).tempDir 
            let filePath = `${filesDir}/test` 
            let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE) 
            let array = new ArrayBuffer(24) 
            fs.readSync(file.fd, array) 
            let res = new Uint8Array(array); 
            console.info(`Demo res:${JSON.stringify(res)}`) 
            for (let i = 0; i < 24; i++) { 
              console.info(`Demo ${res[i]}`) 
            } 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}

DataUtil.ets:

export class DataUtil { 
  static fakeDataArray(): ArrayBuffer { 
    let buf = new ArrayBuffer(8); 
    let res = new Uint8Array(buf); 
    res[0] = 1; 
    res[1] = 2; 
    res[2] = 3; 
    res[3] = 4; 
    res[4] = 5; 
    res[5] = 6; 
    res[6] = 7; 
    res[7] = 8; 
    return buf; 
  } 
}

taskpool方案:

index.ets

@Concurrent 
function writeBuf(filePath: string, buf: ArrayBuffer, offset: number = 0) { 
  console.info(`Demo start:${offset}`) 
  // 新建并打开文件 
  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 
  fs.writeSync(file.fd, buf, { offset }); 
  // 关闭文件 
  fs.closeSync(file); 
  console.info(`Demo end:${offset}`) 
} 
 
export class audioTaskPool { 
  private offset: number = 0 
  private filePath: string = '' 
  private group: taskpool.SequenceRunner = new taskpool.SequenceRunner(); 
  write(path: string, buff: ArrayBuffer) { 
    console.info(Demo enter write:${path} offset:${this.offset} length:${buff.byteLength}) 
    let length = buff.byteLength 
    let task = new taskpool.Task(writeBuf, path, buff, this.offset) 
    this.group.execute(task) 
    this.offset += length 
  } 
} 
... 
private setListener(requestId: string) { 
  let pool:audioTaskPool = new audioTaskPool() 
  ... 
  onData(utteranceId: string, audio: ArrayBuffer, response: textToSpeech.SynthesisResponse) { 
    pool.write(filePath, audio) 
 
  } 
  ... 
}
分享
微博
QQ
微信
回复
2024-08-28 23:20:09
相关问题
如何获知TaskPool执行顺序、优先级?
654浏览 • 0回复 待解决
ArkTS异步机制与执行顺序
1091浏览 • 1回复 待解决
taskpool异步任务支持串行处理方法
877浏览 • 1回复 待解决
如何进行并发操作时保证线程安全
206浏览 • 1回复 待解决
TaskPool如何跟主线程进行通信
399浏览 • 1回复 待解决
HarmonyOS taskpool使用crash
1214浏览 • 2回复 待解决
如何异步方法进行插桩/替换
577浏览 • 1回复 待解决
zip包解压在主线程还是IO线程
1528浏览 • 1回复 待解决
鸿蒙是否进行异步通信?
3785浏览 • 1回复 待解决
如何使用预览接口进行文件预览
290浏览 • 1回复 待解决