• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# I/O Intensive Task Development (TaskPool)
2
3
4I/O intensive tasks are tasks that require frequent I/O operations such as disk read/write and network communication. While asynchronous concurrency can address the thread blocking issue for single I/O tasks, it falls short in the case of I/O intensive tasks. This is where multithread concurrency comes into play.
5
6
7The performance focus of I/O intensive tasks is not the CPU processing capability, but the speed and efficiency of I/O operations, since such a task usually requires frequent operations such as disk read/write and network communication. The following uses frequent read/write operations on a system file to simulate concurrency processing of I/O intensive tasks.
8
9
101. Define a concurrency function that internally calls I/O capabilities intensively.
11    ```ts
12    // a.ts
13    import fs from '@ohos.file.fs';
14
15    // Define a concurrency function that internally calls I/O capabilities intensively.
16    // Write data to the file.
17    export async function write(data: string, filePath: string): Promise<void> {
18      let file: fs.File = await fs.open(filePath, fs.OpenMode.READ_WRITE);
19      await fs.write(file.fd, data);
20      fs.close(file);
21    }
22    ```
23
24	```ts
25    import { write } from './a'
26    import { BusinessError } from '@ohos.base';
27
28    @Concurrent
29    async function concurrentTest(fileList: string[]): Promise<boolean> {
30      // Write data to the file cyclically.
31      for (let i: number = 0; i < fileList.length; i++) {
32        write('Hello World!', fileList[i]).then(() => {
33          console.info(`Succeeded in writing the file. FileList: ${fileList[i]}`);
34        }).catch((err: BusinessError) => {
35          console.error(`Failed to write the file. Code is ${err.code}, message is ${err.message}`)
36          return false;
37        })
38      }
39      return true;
40    }
41	```
42
432. Use **TaskPool** to execute the concurrency function that contains the intensive I/O operations. Specifically, call [execute()](../reference/apis/js-apis-taskpool.md#taskpoolexecute) to execute the tasks and process the scheduling result in a callback. For details about how to obtain **filePath1** and **filePath2** in the example, see [Obtaining Application File Paths](../application-models/application-context-stage.md#obtaining-application-file-paths).
44
45    ```ts
46    import taskpool from '@ohos.taskpool';
47
48    let filePath1: string = "path1"; // Application file path.
49    let filePath2: string = "path2";
50
51    // Use TaskPool to execute the concurrency function that contains the intensive I/O operations.
52    // In the case of a large array, the distribution of I/O intensive tasks also preempts the main thread. Therefore, multiple threads are required.
53    taskpool.execute(concurrentTest, [filePath1, filePath2]).then(() => {
54      // Process the scheduling result.
55    })
56    ```
57