Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
ad7948d1
D
Docs
项目概览
OpenHarmony
/
Docs
大约 1 年 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
ad7948d1
编写于
6月 21, 2023
作者:
O
openharmony_ci
提交者:
Gitee
6月 21, 2023
浏览文件
操作
浏览文件
下载
差异文件
!19893 modify taskpool and errorcode document
Merge pull request !19893 from buzhuyu/master
上级
da34ed09
a0c6f0d0
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
226 addition
and
125 deletion
+226
-125
zh-cn/application-dev/reference/apis/js-apis-taskpool.md
zh-cn/application-dev/reference/apis/js-apis-taskpool.md
+219
-118
zh-cn/application-dev/reference/apis/js-apis-worker.md
zh-cn/application-dev/reference/apis/js-apis-worker.md
+4
-4
zh-cn/application-dev/reference/errorcodes/errorcode-utils.md
...n/application-dev/reference/errorcodes/errorcode-utils.md
+3
-3
未找到文件。
zh-cn/application-dev/reference/apis/js-apis-taskpool.md
浏览文件 @
ad7948d1
...
...
@@ -37,14 +37,13 @@ function printArgs(args) {
console
.
log
(
"
printArgs:
"
+
args
);
return
args
;
}
async
function
taskpoolPriority
()
{
let
task
=
new
taskpool
.
Task
(
printArgs
,
100
);
let
highCount
=
0
;
let
mediumCount
=
0
;
let
lowCount
=
0
;
let
allCount
=
100
;
for
(
let
i
=
0
;
i
<
allCount
;
i
++
)
{
let
task
=
new
taskpool
.
Task
(
printArgs
,
100
);
let
highCount
=
0
;
let
mediumCount
=
0
;
let
lowCount
=
0
;
let
allCount
=
100
;
for
(
let
i
=
0
;
i
<
allCount
;
i
++
)
{
taskpool
.
execute
(
task
,
taskpool
.
Priority
.
LOW
).
then
((
res
:
number
)
=>
{
lowCount
++
;
console
.
log
(
"
taskpool lowCount is :
"
+
lowCount
);
...
...
@@ -63,9 +62,7 @@ async function taskpoolPriority() {
}).
catch
((
e
)
=>
{
console
.
error
(
"
high task error:
"
+
e
);
})
}
}
taskpoolPriority
();
```
## Task
...
...
@@ -107,6 +104,74 @@ function printArgs(args) {
let
task
=
new
taskpool
.
Task
(
printArgs
,
"
this is my first Task
"
);
```
### isCanceled
static isCanceled(): boolean
检查当前正在运行的任务是否已取消。
**系统能力:**
SystemCapability.Utils.Lang
**返回值:**
| 类型 | 说明 |
| ------- | ------------------------------------ |
| boolean | 如果当前正在运行的任务被取消返回true,未被取消返回false。|
**示例:**
```
ts
@
Concurrent
function
inspectStatus
(
arg
)
{
// do something
...
if
(
taskpool
.
Task
.
isCanceled
())
{
console
.
log
(
"
task has been canceled.
"
);
// do something
...
return
arg
+
1
;
}
// do something
...
return
arg
;
}
```
> **说明:**<br/>
> isCanceled方法需要和taskpool.cancel方法搭配使用,如果不调用cancel方法,isCanceled方法默认返回false。
**示例:**
```
ts
@
Concurrent
function
inspectStatus
(
arg
)
{
// 第一时间检查取消并回复
if
(
taskpool
.
Task
.
isCanceled
())
{
console
.
log
(
"
task has been canceled before 2s sleep.
"
);
return
arg
+
2
;
}
// 延时2s
var
t
=
Date
.
now
();
while
(
Date
.
now
()
-
t
<
2000
)
{
continue
;
}
// 第二次检查取消并作出响应
if
(
taskpool
.
Task
.
isCanceled
())
{
console
.
log
(
"
task has been canceled after 2s sleep.
"
);
return
arg
+
3
;
}
return
arg
+
1
;
}
let
task
=
new
taskpool
.
Task
(
inspectStatus
,
100
);
taskpool
.
execute
(
task
).
then
((
res
)
=>
{
console
.
log
(
"
taskpool test result:
"
+
res
);
}).
catch
((
err
)
=>
{
console
.
log
(
"
taskpool test occur error:
"
+
err
);
});
// 不调用cancel,isCanceled()默认返回false,task执行的结果为101
```
### 属性
**系统能力:**
SystemCapability.Utils.Lang
...
...
@@ -144,7 +209,7 @@ execute(func: Function, ...args: unknown[]): Promise\<unknown>
| 错误码ID | 错误信息 |
| -------- | ----------------------------------------- |
| 10200003 | Worker initialization failure. |
| 10200006 |
Serializing an uncaught exception failed
. |
| 10200006 |
An exception occurred during serialization
. |
| 10200014 | The function is not mark as concurrent. |
**示例:**
...
...
@@ -156,12 +221,9 @@ function printArgs(args) {
return
args
;
}
async
function
taskpoolExecute
()
{
let
value
=
await
taskpool
.
execute
(
printArgs
,
100
);
taskpool
.
execute
(
printArgs
,
100
).
then
((
value
)
=>
{
console
.
log
(
"
taskpool result:
"
+
value
);
}
taskpoolExecute
();
});
```
## taskpool.execute
...
...
@@ -192,7 +254,7 @@ execute(task: Task, priority?: Priority): Promise\<unknown>
| 错误码ID | 错误信息 |
| -------- | ----------------------------------------- |
| 10200003 | Worker initialization failure. |
| 10200006 |
Serializing an uncaught exception failed
. |
| 10200006 |
An exception occurred during serialization
. |
| 10200014 | The function is not mark as concurrent. |
**示例:**
...
...
@@ -204,13 +266,10 @@ function printArgs(args) {
return
args
;
}
async
function
taskpoolExecute
()
{
let
task
=
new
taskpool
.
Task
(
printArgs
,
100
);
let
value
=
await
taskpool
.
execute
(
task
);
let
task
=
new
taskpool
.
Task
(
printArgs
,
100
);
taskpool
.
execute
(
task
).
then
((
value
)
=>
{
console
.
log
(
"
taskpool result:
"
+
value
);
}
taskpoolExecute
();
});
```
## taskpool.cancel
...
...
@@ -233,89 +292,51 @@ cancel(task: Task): void
| 错误码ID | 错误信息 |
| -------- | ------------------------- |
| 10200015 |
If the task is not exist
. |
| 10200016 |
If the task is running
. |
| 10200015 |
The task does not exist when it is canceled
. |
| 10200016 |
The task is executing when it is canceled
. |
**
任务取消成功
示例:**
**
正在执行的任务取消
示例:**
```
ts
@
Concurrent
function
printArgs
(
args
)
{
console
.
log
(
"
printArgs:
"
+
args
);
return
args
;
}
async
function
taskpoolCancel
()
{
let
task
=
new
taskpool
.
Task
(
printArgs
,
100
);
taskpool
.
execute
(
task
);
try
{
taskpool
.
cancel
(
task
);
}
catch
(
e
)
{
console
.
log
(
"
taskpool.cancel occur error:
"
+
e
);
function
inspectStatus
(
arg
)
{
// 第一时间检查取消并回复
if
(
taskpool
.
Task
.
isCanceled
())
{
console
.
log
(
"
task has been canceled before 2s sleep.
"
);
return
arg
+
2
;
}
}
taskpoolCancel
();
```
**已执行的任务取消失败示例:**
```
ts
@
Concurrent
function
printArgs
(
args
)
{
console
.
log
(
"
printArgs:
"
+
args
);
return
args
;
}
async
function
taskpoolCancel
()
{
let
task
=
new
taskpool
.
Task
(
printArgs
,
100
);
let
value
=
taskpool
.
execute
(
task
);
let
start
=
new
Date
().
getTime
();
while
(
new
Date
().
getTime
()
-
start
<
1000
)
{
// 延时1s,确保任务已执行
// 2s sleep
var
t
=
Date
.
now
();
while
(
Date
.
now
()
-
t
<
2000
)
{
continue
;
}
try
{
taskpool
.
cancel
(
task
);
//任务已执行,取消失败
}
catch
(
e
)
{
console
.
log
(
"
taskpool.cancel occur error:
"
+
e
);
// 第二次检查取消并作出响应
if
(
taskpool
.
Task
.
isCanceled
())
{
console
.
log
(
"
task has been canceled after 2s sleep.
"
);
return
arg
+
3
;
}
return
arg
+
1
;
}
taskpoolCancel
();
```
**正在执行的任务取消失败示例:**
```
ts
@
Concurrent
function
printArgs
(
args
)
{
console
.
log
(
"
printArgs:
"
+
args
);
return
args
;
}
async
function
taskpoolCancel
()
{
let
task1
=
new
taskpool
.
Task
(
printArgs
,
100
);
let
task2
=
new
taskpool
.
Task
(
printArgs
,
200
);
let
task3
=
new
taskpool
.
Task
(
printArgs
,
300
);
let
task4
=
new
taskpool
.
Task
(
printArgs
,
400
);
let
task5
=
new
taskpool
.
Task
(
printArgs
,
500
);
let
task6
=
new
taskpool
.
Task
(
printArgs
,
600
);
let
res1
=
taskpool
.
execute
(
task1
);
let
res2
=
taskpool
.
execute
(
task2
);
let
res3
=
taskpool
.
execute
(
task3
);
let
res4
=
taskpool
.
execute
(
task4
);
let
res5
=
taskpool
.
execute
(
task5
);
let
res6
=
taskpool
.
execute
(
task6
);
try
{
taskpool
.
cancel
(
task1
);
// task1任务正在执行,取消失败
}
catch
(
e
)
{
console
.
log
(
"
taskpool.cancel occur error:
"
+
e
);
}
}
taskpoolCancel
();
let
task1
=
new
taskpool
.
Task
(
inspectStatus
,
100
);
let
task2
=
new
taskpool
.
Task
(
inspectStatus
,
200
);
let
task3
=
new
taskpool
.
Task
(
inspectStatus
,
300
);
let
task4
=
new
taskpool
.
Task
(
inspectStatus
,
400
);
let
task5
=
new
taskpool
.
Task
(
inspectStatus
,
500
);
let
task6
=
new
taskpool
.
Task
(
inspectStatus
,
600
);
taskpool
.
execute
(
task1
).
then
((
res
)
=>
{
console
.
log
(
"
taskpool test result:
"
+
res
);
}).
catch
((
err
)
=>
{
console
.
log
(
"
taskpool test occur error:
"
+
err
);
});
let
res2
=
taskpool
.
execute
(
task2
);
let
res3
=
taskpool
.
execute
(
task3
);
let
res4
=
taskpool
.
execute
(
task4
);
let
res5
=
taskpool
.
execute
(
task5
);
let
res6
=
taskpool
.
execute
(
task6
);
// 1s后取消task
setTimeout
(()
=>
{
taskpool
.
cancel
(
task1
);},
1000
);
```
## 其他说明
...
...
@@ -441,3 +462,83 @@ import { taskpoolTest1, taskpoolTest2 } from "./c";
func1
();
func2
();
```
**示例五**
```
ts
// 任务取消成功
@
Concurrent
function
inspectStatus
(
arg
)
{
// 第一时间检查取消并回复
if
(
taskpool
.
Task
.
isCanceled
())
{
console
.
log
(
"
task has been canceled before 2s sleep.
"
);
return
arg
+
2
;
}
// 2s sleep
var
t
=
Date
.
now
();
while
(
Date
.
now
()
-
t
<
2000
)
{
continue
;
}
// 第二次检查取消并作出响应
if
(
taskpool
.
Task
.
isCanceled
())
{
console
.
log
(
"
task has been canceled after 2s sleep.
"
);
return
arg
+
3
;
}
return
arg
+
1
;
}
async
function
taskpoolCancel
()
{
let
task
=
new
taskpool
.
Task
(
inspectStatus
,
100
);
taskpool
.
execute
(
task
).
then
((
res
)
=>
{
console
.
log
(
"
taskpool test result:
"
+
res
);
}).
catch
((
err
)
=>
{
console
.
log
(
"
taskpool test occur error:
"
+
err
);
});
// 1s后取消task
setTimeout
(()
=>
{
taskpool
.
cancel
(
task
);},
1000
);
}
taskpoolCancel
();
```
**示例六**
```
ts
// 已执行的任务取消失败
@
Concurrent
function
inspectStatus
(
arg
)
{
// 第一时间检查取消并回复
if
(
taskpool
.
Task
.
isCanceled
())
{
return
arg
+
2
;
}
// 延时2s
var
t
=
Date
.
now
();
while
(
Date
.
now
()
-
t
<
2000
)
{
continue
;
}
// 第二次检查取消并作出响应
if
(
taskpool
.
Task
.
isCanceled
())
{
return
arg
+
3
;
}
return
arg
+
1
;
}
async
function
taskpoolCancel
()
{
let
task
=
new
taskpool
.
Task
(
inspectStatus
,
100
);
taskpool
.
execute
(
task
).
then
((
res
)
=>
{
console
.
log
(
"
taskpool test result:
"
+
res
);
}).
catch
((
err
)
=>
{
console
.
log
(
"
taskpool test occur error:
"
+
err
);
});
setTimeout
(()
=>
{
try
{
taskpool
.
cancel
(
task
);
// 任务已执行,取消失败
}
catch
(
e
)
{
console
.
log
(
"
taskpool.cancel occur error:
"
+
e
);
}
},
3000
);
// 延时3s,确保任务已执行
}
taskpoolCancel
();
```
\ No newline at end of file
zh-cn/application-dev/reference/apis/js-apis-worker.md
浏览文件 @
ad7948d1
...
...
@@ -180,7 +180,7 @@ postMessage(message: Object, transfer: ArrayBuffer[]): void;
| 错误码ID | 错误信息 |
| -------- | ----------------------------------------- |
| 10200004 | Worker instance is not running. |
| 10200006 |
Serializing an uncaught exception failed
. |
| 10200006 |
An exception occurred during serialization
. |
**示例:**
...
...
@@ -213,7 +213,7 @@ postMessage(message: Object, options?: PostMessageOptions): void
| 错误码ID | 错误信息 |
| -------- | ----------------------------------------- |
| 10200004 | Worker instance is not running. |
| 10200006 |
Serializing an uncaught exception failed
. |
| 10200006 |
An exception occurred during serialization
. |
**示例:**
...
...
@@ -866,7 +866,7 @@ Worker线程向宿主线程发送消息。
| 错误码ID | 错误信息 |
| -------- | ----------------------------------------- |
| 10200004 | Worker instance is not running. |
| 10200006 |
Serializing an uncaught exception failed
. |
| 10200006 |
An exception occurred during serialization
. |
**示例:**
...
...
@@ -914,7 +914,7 @@ Worker线程向宿主线程发送消息。
| 错误码ID | 错误信息 |
| -------- | ----------------------------------------- |
| 10200004 | Worker instance is not running. |
| 10200006 |
Serializing an uncaught exception failed
. |
| 10200006 |
An exception occurred during serialization
. |
**示例:**
...
...
zh-cn/application-dev/reference/errorcodes/errorcode-utils.md
浏览文件 @
ad7948d1
...
...
@@ -100,7 +100,7 @@ Worker不支持某API。
**错误信息**
Serializing an uncaught exception failed
.
An exception occurred during serialization
.
**错误描述**
...
...
@@ -246,7 +246,7 @@ Function未被标记为concurrent。
**错误信息**
The task
is not exist when cancel it
.
The task
does not exist when it is canceled
.
**错误描述**
...
...
@@ -264,7 +264,7 @@ The task is not exist when cancel it.
**错误信息**
The task is
running when cancel it
.
The task is
executing when it is canceled
.
**错误描述**
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录