Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Worker Pool for similar tasks #657

Open
butzemannbiber opened this issue Feb 14, 2024 · 1 comment
Open

Worker Pool for similar tasks #657

butzemannbiber opened this issue Feb 14, 2024 · 1 comment

Comments

@butzemannbiber
Copy link

I would like to use a worker pool to delegate tasks to multiple workers with the same interface.

Without Comlink: When there is a new task to be done, the pool selects a worker that is currently idle and assigns the task to it. For the time a worker is processing a task i mark the worker as "busy" - if the work is done i mark the worker as idle. Its working fine but i need to write my own message protocol ending up writing long switch-case statements

With Comlink: The idea is to request a "free" worker from the worker pool (using a Promise), execute an async function, and process the result in the form of a return value. I can mark the requested worker as "busy" but i have problems mark is as "idle" after returning the value.
Unfortunately, I have not found a way to determine whether or how many "messages" are currently being processed by a worker. So, whether a function is currently being executed via Comlink.

Do you have any idea on how I could solve the problem?

@butzemannbiber
Copy link
Author

butzemannbiber commented Mar 19, 2024

Just in case somebody else is interested - i ended up using the following concept:

WP - Worker-Pool (n comlink worker instances)

  1. Request side: Query WP for a - not busy - worker instance and get a promise for the worker
  2. WP: marks a - not busy - worker as busy and resolve the promise
  3. Request side: do what ever you want with the worker
  4. Request side: if your work is done call a release method at the WP to remove the "busy" flag from the worker

This is a somewhat cumbersome way but aslong as the worker ist getting released back to the pool at the end - its working.

If there is a need for heavy parallel processing and you have a bunch of tasks you can easily use a n-sized worker pool and make sure every available thread is doing as much work as possible (instead of using a distribution algo like round robin)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant