1

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;
    }
}
3
  • It may help if you could construct a minimal reproducible example and include it in this question. Commented Jan 5, 2024 at 12:27
  • Hello Joshua, I also got update from PISCINA discussions that we can change the maxThread value. I'm testing the solution by tweaking the value to max possible. after some thread number there is no improvement in performance. Commented Jan 6, 2024 at 14:18
  • Interesting - when did the drop off in performance happen? It may be good to add this research and your results to the question as an edit. I don't think I have the expertise to help you, but someone else may. Best of luck! Commented Jan 6, 2024 at 16:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.