好的,所以我有一个用Node.js编写的API,该API进行了集群化,以利用“集群”模块为所有4个子进程使用所有可用的内核。
const cluster = require('cluster');
我有一个进程('remNodeProcess'),我只需要派生一个实例,即我不希望每个工作节点派生自己的'remNodeProcess'实例。此“ remNodeProcess”并不需要对每个子节点都可用。为此,我正在尝试使用“子进程”模块。
const childProcess = require('child_process');
我对Node Clusters有了很好的阅读,并且已经使用了一段时间。群集按预期工作,并且获得了预期的结果,但似乎无法弄清楚如何派生一个流程的单个实例。我只希望集群中有1个实例。
这是要集群的完整代码:
const setupWorkerThreadNodes = () => {
// Get system CPUs
let SYS_CORE_COUNT = require('os').cpus().length;
// Read the Config Thread Configuration, if it exceeds the SYS_CORE_COUNT, set it to max usage, otherwise honour the Config
let coreCount;
if (webAppConfig.appConfig.nodeClusterConfiguration.mtMaxWorkerThreadCount) {
coreCount = webAppConfig.appConfig.nodeClusterConfiguration.mtMaxWorkerThreadCount;
} else if (webAppConfig.appConfig.nodeClusterConfiguration.mtMaxWorkerThreadCount > SYS_CORE_COUNT) {
coreCount = SYS_CORE_COUNT;
}
logMsg(`NODE_MASTER PID: ${process.pid}`);
console.log('NODE_CLUSTER Master is setting up Nodes... ');
// For each Core, create a node and push it to the system cluster array
for (let i = 0; i < coreCount; i++) {
console.log(`NODE_CLUSTER Master is setting up Node [${i}/${coreCount}]... `);
workers.push(cluster.fork({ WorkerName: `Worker${i}` }));
// When a Worker responds to the Master, log the response
workers[i].on('message', function (message) {
console.log(message);
});
}
// process is clustered on a core and process id is assigned
cluster.on('online', function (worker) {
console.log('Worker ' + worker.process.pid + ' is listening');
logMsg(`CLUSTER_NODE Child opened. PID: ${worker.process.pid}`)
});
// If the Workers count matches the CoreCount, we've opened the correct amount of threads
if (workers.length === coreCount) logMsg(`NODE_CLUSTER Array successfully established.`, 'info');
// if any of the worker process dies then start a new one by simply forking another one
cluster.on('exit', function (worker, code, signal) {
console.log('Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal);
logMsg(`CLUSTER_NODE Child terminated. PID: ${worker.process.pid}. Code: ${code}. Signal: ${signal}`)
console.log('Starting a new worker');
cluster.fork();
workers.push(cluster.fork());
// to receive messages from worker process
workers[workers.length - 1].on('message', function (message) {
console.log(message);
});
});
};
我尝试了以下操作:
if(cluster.isMaster){
const remNodeProcess= childProcess.fork('./background_workers/remNode.js');
remNodeProcess.send(pool, (err) => { console.log(err) })
}
当我使用此代码运行API时,remNodeProcess无法打开。如果我使用以下代码(不使用IF包装器):
const remNodeProcess= childProcess.fork('./background_workers/remNode.js');
remNodeProcess.send(pool, (err) => { console.log(err) })
我为每个孩子得到一个remNodeProcess。
如何派生给定进程的一个实例?可以由师父来完成吗?
答案 0 :(得分:0)
我知道了。
如果当前工人的WorkerName为Worker0,则它将手动派生Reminder子代。结果仅是集群中remNode的1个实例,而不是4个。
if (process.env.WorkerName === "Worker0") {
console.log(`${process.env.WorkerName} forking ReminderNode...`)
remNodeProcess = childProcess.fork('./background_workers/remNode.js')
.on('message', msg => {
logMsg(msg)
});
}