Hello, I'm learning how to do parallel threading in Nodejs, which is by design single-threaded. I have a question about queue concurrency.
For context to my question, consider this queue example:
var q = async.queue(function(task, callback) {
console.log('hello ' + task.name);
callback();
}, 2); // <-- concurrency greater than 1
Does a concurrency value of greater than 1 take advantage of a machine having multiple CPU cores? Or does concurrency in an async/queue happen in a single thread/process Event Loop?
I'm trying to compare how async/queue's concurrency parameter is the same or different than hunterloftis/throng module's use of the Nodejs Cluster API to fork the process per the "concurrency" number I provide to it.
Thanks!
I think the answer is async/queue uses the same Nodejs process regardless the concurrency number. And if each concurrent worker is on the same process then they (guessing here) are all working on and sharing the same CPU core / thread / event loop.
throng, which uses the Cluster API, forks child processes and each is assigned their own process id. And since each has their own process then they can be handled on different CPU cores.
// index.js
const async = require('async');
const throng = require('throng');
let queue;
throng({
workers: 4,
lifetime: 0,
master: () => {
console.log('master');
queue = async.queue(function(task, callback) {
console.log(`hello ${task.name}, pid=${process.pid}`);
callback();
}, 4);
for (let i = 0; i < 10; i++) {
queue.push({ name: i });
}
},
start: (workerId) => {
console.log(`workerId=${workerId}, pid=${process.pid}`);
},
});
$ node index.js
master
hello 0, pid=21587
hello 1, pid=21587
hello 2, pid=21587
hello 3, pid=21587
hello 4, pid=21587
hello 5, pid=21587
hello 6, pid=21587
hello 7, pid=21587
hello 8, pid=21587
hello 9, pid=21587
workerId=1, pid=21588
workerId=4, pid=21591
workerId=2, pid=21589
workerId=3, pid=21590
if anyone is interested on how to use the number of cpus for the concurrency limit here is a way to get the current number of cpus
// from https://github.com/piscinajs/piscina/blob/master/src/index.ts
const { cpus } = require('os')
const async = require('async')
const cpuCount = () => {
try {
return cpus().length
} catch {
return 1
}
}
const q = async.queue(function(task, callback) {
console.log('hello ' + task.name);
callback();
}, cpuCount()); // <-- concurrency based on number of cpus
Most helpful comment
I think the answer is
async/queueuses the same Nodejs process regardless the concurrency number. And if each concurrent worker is on the same process then they (guessing here) are all working on and sharing the same CPU core / thread / event loop.throng, which uses the Cluster API, forks child processes and each is assigned their own process id. And since each has their own process then they can be handled on different CPU cores.Sample Code
Output