Bird
0
0

What is wrong with this worker pool implementation?

medium📝 Debug Q7 of 15
Node.js - Worker Threads
What is wrong with this worker pool implementation?
const { Worker } = require('worker_threads');

function runTask() {
  const worker = new Worker('./worker.js');
  worker.on('message', result => console.log(result));
}

for (let i = 0; i < 10; i++) {
  runTask();
}
AIt uses a synchronous loop blocking the event loop
BIt creates a new worker for each task, which is inefficient
CIt does not listen for 'error' events on workers
DIt should use child_process instead of worker_threads
Step-by-Step Solution
Solution:
  1. Step 1: Analyze worker creation in loop

    The code creates a new worker for each task, which can be costly in resources.
  2. Step 2: Identify inefficiency

    Worker pool pattern reuses a fixed number of workers instead of creating many.
  3. Final Answer:

    It creates a new worker for each task, which is inefficient -> Option B
  4. Quick Check:

    Worker creation per task = Inefficient resource use [OK]
Quick Trick: Reuse workers instead of creating many for efficiency [OK]
Common Mistakes:
  • Ignoring error event listeners
  • Thinking synchronous loop blocks event loop here
  • Confusing worker_threads with child_process

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes