以nestjs为例,如果有这样两个路由A和B,其中A是给前端提供数据展示的,B是长任务,比如解压很大的压缩包。如果不开启子进程,那执行B操作就会占用主进程资源,让A访问变慢,此时就要把B踢到子进程去执行。
app.controller.ts:
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello() {
return this.appService.getHello();
}
@Get('longtask')
async doLongTask() {
const res:any = await this.appService.startLongTask({ id: 10 });
return res;
}
}
app.service.ts:
import { Injectable } from '@nestjs/common';
import { fork, ChildProcess } from 'child_process';
@Injectable()
export class AppService {
private worker: ChildProcess;
getHello(){
return {code:0,success:true,msg:'show now!'}
}
startLongTask(data: any) {
return new Promise(resolve=>{
this.worker = fork('dist/longTask.js');
this.worker.send({ type: 'start', data });
// 接收子进程发送的消息
this.worker.on('message', (result) => {
console.log('子进程返回的结果:', result);
resolve(result);
});
// 监听子进程的退出事件
this.worker.on('exit', (code, signal) => {
console.log(`子进程已退出,退出码:${code},信号:${signal}`);
this.worker.removeAllListeners();// 取消事件监听器以释放资源
this.worker = null;
});
});
}
}
子进程可以是ts,不过访问的路径应该是dist目录下的js文件,这是实时编译的。
longTask.ts:
// longTask.js
process.on('message', (message: any) => {
console.log('子进程接收到的信息>>>', message);
if (message.type === 'start') {
// 执行长任务
setTimeout(() => {
const result = { code: 0, success: true, data: message.data };
process.send(result);
}, 10000);
// 完成后将结果发送回主进程
}
});
网友评论