Challenge - 5 Problems
Worker Pool Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this worker pool code snippet?
Consider this Node.js worker pool code using the 'worker_threads' module. What will be logged to the console?
Node.js
import { Worker, isMainThread, parentPort } from 'worker_threads'; if (isMainThread) { const worker = new Worker(new URL(import.meta.url)); worker.on('message', (msg) => console.log('Main thread received:', msg)); worker.postMessage('start'); } else { parentPort.on('message', (msg) => { if (msg === 'start') { parentPort.postMessage('worker done'); } }); }
Attempts:
2 left
💡 Hint
Focus on the message events between main thread and worker.
✗ Incorrect
The main thread creates a worker that listens for a 'start' message. When received, the worker sends back 'worker done'. The main thread logs this message.
❓ state_output
intermediate2:00remaining
How many tasks are processed concurrently in this worker pool?
Given this simplified worker pool code, how many tasks run at the same time?
Node.js
import { Worker } from 'worker_threads'; class WorkerPool { constructor(size) { this.size = size; this.workers = []; this.queue = []; this.active = 0; } runTask(task) { if (this.active < this.size) { this.active++; const worker = new Worker(task); worker.on('exit', () => { this.active--; if (this.queue.length) this.runTask(this.queue.shift()); }); this.workers.push(worker); } else { this.queue.push(task); } } } const pool = new WorkerPool(3); pool.runTask('task1.js'); pool.runTask('task2.js'); pool.runTask('task3.js'); pool.runTask('task4.js');
Attempts:
2 left
💡 Hint
Check the active count compared to pool size.
✗ Incorrect
The pool size is 3, so up to 3 workers run tasks simultaneously. The 4th task waits in the queue until a worker finishes.
🔧 Debug
advanced2:30remaining
Why does this worker pool code cause a memory leak?
Identify the cause of the memory leak in this worker pool snippet.
Node.js
import { Worker } from 'worker_threads'; class Pool { constructor(size) { this.size = size; this.workers = []; } run(task) { const worker = new Worker(task); this.workers.push(worker); worker.on('exit', () => { console.log('Worker exited'); }); } } const pool = new Pool(2); pool.run('task1.js'); pool.run('task2.js'); pool.run('task3.js');
Attempts:
2 left
💡 Hint
Check how the workers array is managed after workers exit.
✗ Incorrect
The workers array keeps references to all workers even after they exit, preventing garbage collection and causing memory to grow.
📝 Syntax
advanced2:00remaining
Which option correctly creates a worker pool with 4 workers?
Select the code snippet that correctly initializes a worker pool with 4 workers using 'worker_threads'.
Attempts:
2 left
💡 Hint
Remember how Array.fill() and Array.map() behave differently.
✗ Incorrect
Option B creates 4 distinct Worker instances. Option B fills with the same Worker reference. Option B fails because forEach returns undefined, not a new array. Option B is invalid syntax.
🧠 Conceptual
expert2:30remaining
What is the main advantage of using a worker pool in Node.js?
Choose the best explanation for why a worker pool is used in Node.js applications.
Attempts:
2 left
💡 Hint
Think about resource management and task handling.
✗ Incorrect
Worker pools limit thread creation overhead by reusing a fixed number of workers, improving efficiency and performance.