Sobes.tech
Junior — Middle

How did you organize work with worker threads in your projects?

sobes.tech AI

Answer from AI

In Node.js projects, to manage worker threads, I usually use the worker_threads module or third-party libraries, such as bull for task queues.

Main approaches:

  • Creating a pool of workers to limit the number of threads working simultaneously.
  • Sending tasks to workers via messages.
  • Handling results and errors asynchronously.

Example using worker_threads:

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

function runWorker(path, workerData) {
  return new Promise((resolve, reject) => {
    const worker = new Worker(path, { workerData });
    worker.on('message', resolve);
    worker.on('error', reject);
    worker.on('exit', code => {
      if (code !== 0) reject(new Error(`Worker stopped with exit code ${code}`));
    });
  });
}

(async () => {
  try {
    const result = await runWorker('./worker.js', { some: 'data' });
    console.log('Result from worker:', result);
  } catch (err) {
    console.error(err);
  }
})();

This approach allows for efficient load distribution and does not block the main thread.