【发布时间】:2020-02-04 09:19:54
【问题描述】:
我是节点 js 的新手。我编写了一个脚本,它产生 5 个子进程并从中捕获日志, 到目前为止,日志看起来很乱。
但我想以更清晰的格式显示输出,例如每行都被重用,数据被记录在进程 ID 旁边。 像这样的东西:
下面是我的代码:
var state = "node stealth.js ";
var exec = require('child_process').exec;
workers = 5
p_list = [];
for (var i = 0; i < workers; i++) {
(function(i){
var child = exec(state+'user'+i);
console.log('started Worker '+i)
// Add the child process to the list for tracking
p_list.push({process:child, content:""});
// Listen for any response:
child.stdout.on('data', function (data) {
console.log(child.pid, data);
p_list[i].content += data;
process.stdout.cursorTo(i);
process.stdout.clearLine();
process.stdout.write(child.pid+data);
process.stdout.write("\n"); // end the line
});
// Listen for any errors:
child.stderr.on('data', function (data) {
console.log(child.pid, data);
p_list[i].content += data;
});
// Listen if the process closed
child.on('close', function(exit_code) {
console.log('Closed before stop: Closing code: ', exit_code);
});
})(i)
}
【问题讨论】:
标签: node.js concurrency console-application puppeteer