任务
执行计算密集型业务或耗时业务时,你可以用job来处理。
# 任务 / 并发任务
// frontend/src/views/framework/jobs/Index.vue
<script>
runJob(jobId, operation) {
let params = {
id: jobId,
type: 'timer',
action: operation
}
this.$ipc.invoke(ipcApiRoute.someJob, params).then(data => {
switch (data.jobId) {
case 1:
if (data.action == 'create') {
this.progress1_pid = data.result.pid;
}
break;
case 2:
if (data.action == 'create') {
this.progress2_pid = data.result.pid;
}
break;
}
})
},
</script>
// electron/controller/framework.js
/**
* 任务
*/
someJob(args, event) {
let jobId = args.id;
let action = args.action;
let result;
switch (action) {
case 'create':
result = this.service.framework.doJob(jobId, action, event);
break;
case 'close':
this.service.framework.doJob(jobId, action, event);
break;
default:
}
let data = {
jobId,
action,
result
}
return data;
}
// Make sure to add code blocks to your code group
# 任务池 / 并发任务
// frontend/src/views/framework/jobs/Index.vue
<script>
createPool() {
let params = {
number: 3,
}
this.$ipc.send(ipcApiRoute.createPool, params);
},
runJobByPool(jobId, operation) {
let params = {
id: jobId,
type: 'timer',
action: operation
}
this.$ipc.invoke(ipcApiRoute.someJobByPool, params).then(data => {
switch (data.jobId) {
case 3:
if (data.action == 'run') {
this.progress3_pid = data.result.pid;
}
break;
case 4:
if (data.action == 'run') {
this.progress4_pid = data.result.pid;
}
break;
}
})
},
</script>
// electron/controller/framework.js
/**
* 创建任务池
*/
async createPool(args, event) {
let num = args.number;
this.service.framework.doCreatePool(num, event);
return;
}
/**
* 通过进程池执行任务
*/
someJobByPool(args, event) {
let jobId = args.id;
let action = args.action;
let result;
switch (action) {
case 'run':
result = this.service.framework.doJobByPool(jobId, action, event);
break;
default:
}
let data = {
jobId,
action,
result
}
return data;
}
// Make sure to add code blocks to your code group
# 完整代码
上次更新: 2025/04/10, 03:07:49