I have a question which I can't find in the documentation. If I create a nodejs application using beanstalk and use multicore machines, will beanstalk utilize all of these cores? Since nodejs is a single threaded application, will beanstalk then create a new instance of nodejs for each cpu? How does this exactly work?
Cheers
No, Beanstalk fires only one nodejs process, using only one core. To take advantage of multi-core machines, the best practice is to use the cluster module. It allows to fork your process as many times as you want. From the doc:
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
// Workers can share any TCP connection
// In this case it is an HTTP server
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With