简体   繁体   中英

Multithreaded API - NodeJS

I'd like to know if there is possible to build a multithreaded API using NodeJS. I have read about worker_threads and seems like it's what I'm looking for.

I'm trying to have an endpoint that processes requests with multiple threads.

const { Worker } = require('worker_threads');

// this is my /insert endpoint
const insert = async (ctx, req) => {
    const records = req.data;

    try {
        const port = new Worker('./worker.js', {workerData : {records: records}});
        port.on('message', console.log);
    } catch (err) {
            console.log(err);
    }
};

And this is my Worker.js

const {parentPort, workerData} = require('worker_threads');
const {insertProfile} = require('../repository');


(async ({records}) => {
    const results = await Promise.all(records.map(async (records) => {
        return insertProfile(records);
    }));
    parentPort.postMessage(results.length);
    // return results.length;
})({records: workerData.records});

As you can see in every request I'm creating a new Worker . That's not optional since I expect thousands of requests so in a while my responses become very slow. What I want to do is to have for example 10 threads Worker and reuse them in every request, is this possible in NodeJS?

You can use BullMQ npm package to use queue of worker threads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM