josdejong / workerpool

Offload tasks to a pool of workers on node.js and in the browser
Apache License 2.0
2.04k stars 148 forks source link

[fork-join] Can / How assign task to workers #422

Closed riskers closed 8 months ago

riskers commented 8 months ago

It's a wonderful lib for workers!

But Can / How it assign task to workers?

<button id="test">test</button>

<script src="https://www.unpkg.com/workerpool@9.0.3/dist/workerpool.js"></script>
<script>
  const pool = workerpool.pool('./worker.js', {
    maxWorkers: 7,
    minWorkers: 3,
  })

  document.getElementById('test').onclick = async () => {
    const res = await pool.exec('log', [123])
    console.log('res',  res)
  }
</script>
// worker.js
importScripts('https://www.unpkg.com/workerpool@9.0.3/dist/workerpool.js')

// calacute function
function log(data) {
  console.log(data);
  return data;
}

workerpool.worker({
  log: log
})

SandboxCode ->

I see that workerpool.pool is executed and 3 workers are created:

image

There is always only one worker executing when I clicked button:

image image

Can each worker perform the task equally?


Because now I have an application scenario:

const pool = workerpool.pool({
  maxWorkers: 7,
  minWorkers: 3,
})

const tasks = [..] // many data
const promiseList = []
for (let i = 0; i < tasks.length; i++) {
  const exec = pool.exec('log', [tasks[i]])
  promiseList.push(exec)
}

const res = await Promise.all(promiseList)

But only one worker is executing the task. Am I using it wrong?

I thought it would be possible to execute multiple tasks in multiple workers at the same time and then aggregate the results, like fork join.

josdejong commented 8 months ago

I thought it would be possible to execute multiple tasks in multiple workers at the same time and then aggregate the results, like fork join.

The core feature of workerpool is that you can execute multiple tasks in parallel, a nice example to try this out is examples/browser/dedicatedWorker.html. If you have a big task that you can split in smaller parts, you can indeed execute the subtasks in parallel and in the end aggregate the results like in your example, you last example with the await Promise.all should indeed work.

riskers commented 8 months ago

why is there always only one worker execute?

josdejong commented 8 months ago

That is not the case.

riskers commented 8 months ago

1

codesandbox There is only a second worker executing

josdejong commented 8 months ago

workerpool only creates more workers when needed: when a new task is added whilst all created workers are busy (and maxWorkers isn't reached). The tasks in your example take about no time, so I guess that case does not happen in your example. To see more workers being created, change your tasks to something that takes say 10 seconds (for example return a Promise that resolves after 10 seconds using a setTimeout).

riskers commented 8 months ago

Oh, I see.

Thank you for your patience 😄

riskers commented 8 months ago

Here is demo.

If anyone meets the same question.