启动后分离一个 spawn 子进程

Ops*_*sse 10 multithreading spawn node.js

我以这种方式启动一个 spawn 子进程:

let process = spawn(apiPath, {
  detached: true
})

process.unref()

process.stdout.on('data', data => { /* do something */ })
Run Code Online (Sandbox Code Playgroud)

当我开始这个过程时,我需要保持它的连接,因为我想读取它的输出。但就在关闭我的 Node 进程(父进程)之前,我想分离所有未完成的子进程以保持它们在后台运行,但正如文档所说:

当使用 detached 选项启动一个长时间运行的进程时,进程在父进程退出后不会一直在后台运行,除非它提供了一个没有连接到父进程的 stdio 配置。

但是有了这个选项,stdio: 'ignore'我无法阅读stdout这是一个问题。

我试图在关闭父进程之前手动关闭管道,但没有成功:

// Trigger just before the main process end
process.stdin.end()
process.stderr.unpipe()
process.stdout.unpipe()
Run Code Online (Sandbox Code Playgroud)

Ops*_*sse 1

经过多次测试,我发现至少有一种方法可以解决这个问题:在离开主进程之前销毁所有管道。

一个棘手的问题是子进程必须正确处理管道破坏,否则它可能会出错并关闭。在这个例子中,节点子进程似乎没有问题,但它可能与其他场景不同。

main.js

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)
Run Code Online (Sandbox Code Playgroud)

孩子.js

console.log('Start Child')

setInterval(function() {
   process.stdout.write('hello from child')
}, 1000)
Run Code Online (Sandbox Code Playgroud)

输出

启动主程序
获取数据:启动子程序

获取数据:来自子项的
你好 获取数据:来自子项的
你好 获取数据:来自子项的
你好 获取数据:来自子项的你好
断开子项连接