watson / worker-threads-pool

Easily manage a pool of Node.js Worker Threads
MIT License
427 stars 27 forks source link

The question about the example? #5

Open chengdonglin opened 5 years ago

chengdonglin commented 5 years ago

Some question about the example? the example you give the tasks to run by thread pool, if the tasks number is dynamic, how to resolve this problem? The following code:

const Pool = require('worker-threads-pool') const pool = new Pool({max: 5}) const tasks=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17] for (let i = 0; i < tasks.length; i++) { pool.acquire('./worker.js',{workerData:tasks[i]}, function (err, worker) { if (err) throw err console.log(started worker ${i} (pool size: ${pool.size})) worker.on('exit', function () { console.log(worker ${i} exited (pool size: ${pool.size})) }) }) } console.log("main thread")

thanks!

x-077 commented 5 years ago

Hello,

Did you find a solution ?

Thanks

chengdonglin commented 5 years ago

Hello,

Did you find a solution ?

Thanks

you can use the queue to save the tasks, and then polling the tasks, when complete the task, pop this task from the queue.

waldemarnt commented 5 years ago

It's old but let me add my 50 cents. The getting started is a bit confusing but the code implements a queue, you don't need that for https://github.com/watson/worker-threads-pool/blob/master/index.js#L29

Here's my implementation if you wanna have a look https://github.com/waldemarnt/node-docker-example/blob/master/src/workers/fibonacciWorkerPool.js