我以这种方式启动生成子进程:
let process = spawn(apiPath, {
detached: true
})
process.unref()
process.stdout.on('data', data => { /* do something */ })
当我开始该过程时,我需要使其保持连接状态,因为我想读取其输出。但是在关闭我的Node进程(父进程)之前,我想分离所有未完成的子进程以使其在后台运行,但是正如文档所述:
使用分离选项启动长时间运行的进程时,除非父进程没有提供未连接到父进程的stdio配置,否则该进程将在父进程退出后不会在后台继续运行。
但是,使用该选项,stdio: 'ignore'
我无法读取,stdout
这是一个问题。
我尝试在关闭父进程之前手动关闭管道,但未成功:
// Trigger just before the main process end
process.stdin.end()
process.stderr.unpipe()
process.stdout.unpipe()
经过多次测试,我发现了至少一种解决此问题的方法:在离开主流程之前销毁所有管道。
棘手的一点是,子进程必须正确处理销毁管道的情况,否则可能会出错并仍然关闭。在此示例中,节点子进程似乎对此没有问题,但与其他情况可能有所不同。
const { spawn } = require('child_process')
console.log('Start Main')
let child = spawn('node', ['child.js'], { detached: true })
child.unref() // With this the main process end after fully disconnect the child
child.stdout.on('data', data => {
console.log(`Got data : ${data}`)
})
// In real case should be triggered just before the end of the main process
setTimeout(() => {
console.log('Disconnect the child')
child.stderr.unpipe()
child.stderr.destroy()
child.stdout.unpipe()
child.stdout.destroy()
child.stdin.end()
child.stdin.destroy()
}, 5000)
console.log('Start Child')
setInterval(function() {
process.stdout.write('hello from child')
}, 1000)
开始主要
数据:开始子获得数据:来自子项的问候
获得数据:来自子项的问候
获得数据:来自子项的问候
获得数据:来自子项的问候
断开子项的连接
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句