Open QuisMagni opened 7 months ago
Just in case somebody else is interested - i ended up using the following concept:
WP - Worker-Pool (n comlink worker instances)
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)
I have developed a worker pool (WP) functionality. This WP is relatively simple because my requirements do not involve coordinating multiple WPs.
The main functional design of this WP is as follows:
The WP has the following shortcomings:
This WP creates many promises to handle asynchronous operations and intermediate steps. Even your business logic must forcibly return a promise object. If a promise chain is not handled correctly somewhere (e.g., forgetting to return a promise or incomplete error handling), it may cause a thread to get stuck or the main business logic to get stuck. Although this issue can be mitigated by forcibly rejecting with a timeout, I have not implemented this.
In the check_task_queue method, it checks the task queue and the idle worker pool and assigns tasks to idle workers. However, if a worker processes tasks very quickly (or very slowly), it may lead to unbalanced task assignment, especially when the task arrival rate is uneven. I am currently trying to resolve this issue.
I have not yet thought too deeply about other issues. I hope this is useful to you.
codes in main.js
import parallel_process from '../worker'
'const abc = async function(param1,param2,...){ ..... return ......}'
const result = await parallel_process(abc,[param1,param2,...])
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?