Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

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 ');

    }

}

a number of cores are always 2, but I never see the child process log.

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);

}

closed

1 Answer

0 votes
by (44.4k points)
selected by
 
Best answer

As you can see, exports.test function is not invoked for all the child processes. A new instance of the file with different parameters is invoked using cluster.fork().

AWS Lambda will invoke the function which runs the parent process, but the child process defines the function and waits afterwards. 

Making cluster.isMaster check happens everywhere in the code will make it work:

'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();

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

Browse Categories

...