AWS Lambda上的NodeJS集群 [英] NodeJS Cluster on AWS Lambda

查看:123
本文介绍了AWS Lambda上的NodeJS集群的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在lambda函数中使用集群模块?我试过了:

Is it possible to use the cluster module in lambda functions? I tried this:

'use strict';
var cluster = require('cluster');  
var http    = require('http');  
var os      = require('os');

var numCPUs = os.cpus().length;
console.log('Number of Cores : ', numCPUs);

exports.test = (event, context, callback) => {
    if (cluster.isMaster) {
        for (var i = 0; i < numCPUs; ++i) {
            cluster.fork();
        }
    } else {
        console.log('child process ');
    }
}

核心数始终为2,但我从未看到子进程日志.

number of cores is always 2, but i never see the child process log.

更新评论示例:

我尝试实现消息模式,但仍然没有收到孩子发送的消息. for循环正确地在集群工作程序中循环,但从未找到消息.

I tried implementing the message pattern but i'm still not receiving the message sent by the children. The for loop correctly loops through the cluster workers but never finds a message.

'use strict';
var cluster = require('cluster');  
var http    = require('http');  
var os      = require('os');

var numCPUs = os.cpus().length;
console.log('Number of Cores : ', numCPUs);

exports.test = (event, context, callback) => {
    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);
}

推荐答案

问题似乎是没有任何东西可以为每个子进程调用exports.test函数. cluster.fork()从头开始调用具有不同参数的文件新实例,这与 C 中的fork()系统调用不同,后者会克隆当前进程并从两个父目录的同一行继续和子进程.

The issue appears to be that there's nothing to invoke the exports.test function for each of the child processes. cluster.fork() invokes a new instance of the file with different parameters that starts from the beginning, , unlike the fork() system call in C which clones the current process and continues from the same line in both parent and child process.

对于父进程,AWS lambda将调用此函数,但是子进程仅定义该函数,然后等待.

For the parent process, AWS lambda will invoke this function, but the child process just defines the function and then waits.

我会翻转您的逻辑,以便cluster.isMaster检查发生在其他所有内容上;我已经在本地测试了以下内容:

I'd flip your logic around so that the cluster.isMaster check happens around everything else; I've tested the following locally:

'use strict';
let cluster = require('cluster')
let http = require('http')
let os = require('os')

let numCPUs = os.cpus().length

if (cluster.isMaster) {
  console.log('Number of Cores : ', numCPUs);
  exports.test = (event, context, callback) => {
    for (let 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);
}

// The following used for local test

exports.test && exports.test();

这篇关于AWS Lambda上的NodeJS集群的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆