How many threads Node creates per core when using worker threads?

61 Views Asked by At

I'm running node js worker threads on MacBook Air M1. There are 8 core. And using PISCINA to create threads. I'm getting 12 threads. Are those for one core or on all 8 core. How do I know if main thread is blocked/being used in worker threads when running workers for all threads?

When running on all threads, other requests are getting blocked. I want to use all cores and full capacity of threads without blocking main thread.

Also, when I see the CPU utilisation when worker threads are running, its not crossing above 30%. It looks like threads are spawned on single core.

//main thread
const Piscina = require('piscina');
const path = require('path');
const _ = require("lodash");


const testpscn = new Piscina({
    filename: path.resolve('path_to_worker_js' + `worker.js`),
    maxThreads: 12        //1.5 * number of core (M1 its 1.5 * 8 = 12)
});

exports.doCPUIntensiveTask = async (inpt) => {
    try {
        let response=[];
        let thrd = testpscn.maxThreads;
        for (const o of _.range(thrd)) {
            const result = await testpscn.run(inpt[o]);
            if(result !=-1){
                response =[...response,...result]
            }
        }
        return response;
    } catch (e) {
        logger.error(e.stack);
        return -1;
    }
};

//worker.js
module.exports = async (inpt) => {
try {
      let rs = [] // cpu intnsive task that generate array of object as response
      return rs;
    } catch (e) {
        logger.error(e.stack);
        return -1;
    }
}
0

There are 0 best solutions below