process-worker-executor
Version:
将多进程和多线程封装为统一的 Async/Await 调用方式, 简化 IPC 消息通信的复杂性
26 lines (23 loc) • 781 B
JavaScript
const { fork } = require('child_process')
const { join } = require('path')
const Executor = require('../Executor')
class ProcessExecutor extends Executor {
#process
async onInit() {
const modulePath = join(__dirname, 'ProcessExecutorLoder.js')
const child = fork(modulePath, [this.logPath, this.logFile], {
stdio: 'inherit'
})
child.on('message', this.on.bind(this))
child.on('exit', this.close.bind(this))
this.#process = child
this.disposable.push(() => {
child.kill('SIGINT')
})
this.logger?.info('创建进程成功, 进程: %s', child.pid)
}
async onSend(data) {
this.#process.send(data)
}
}
module.exports = ProcessExecutor