NodeJS递归调用:spawnSync()不打印console.log()

时间:2019-07-02 07:26:35

标签: node.js

我有一个节点进程,它使用spawnSync()对其自身进行递归调用。

console.log('hello there')将仅打印来自父进程的消息。

test.js

const CHILD_PROCESS = require('child_process') // https://nodejs.org/api/child_process.html

console.log('hello there', i)
var i = process.argv[2]-1

// recursive call
if (i > 0) CHILD_PROCESS.spawnSync('node', [process.argv[1], i])

命令行(产生10个孩子):

node test.js 10

我尝试使用process.stdout(),但仍然只打印父节点消息。

child_process.fork()在这种情况下不适用,因为它必须是同步过程。

https://nodejs.org/api/child_process.html

如何生成程序以在控制台中打印其消息?

0 个答案:

没有答案