我写了这个简短的例子来测试AWS lambda中的集群。谁能告诉我哪里出错了?核心数返回2,但消息永远不会传递。
'use strict';
var cluster = require('cluster');
var http = require('http');
var os = require('os');
var numCPUs = os.cpus().length;
exports.test = (event, context, callback) => {
console.log('Number of Cores : ', numCPUs);
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; ++i) {
cluster.fork();
}
for (const id in cluster.workers) {
cluster.workers[id].on('message', messageHandler);
}
} else {
process.send('running');
}
};
function messageHandler(msg) {
console.log(msg);
}