I'm currently writing on a program, where I have to deal with huge arrays. I can however split those arrays. My plan now is, to process the arrays in different web workers. I have however never worked with them and do have several questions:
1. How would I run several web workers? I tried a for-loop looking like that:
for(i = 0; i < eD.threads; i++){
//start workers here
var worker = new Worker("js/worker/imageValues.js");
worker.postMessage(brightness, cD.pixels[i]);
}
Here I do get the error, that the object couldn't be cloned. Which seems logical. I guess it would be better to save them in an Array?
2. How would I control that all have finished their work? (I need to reassembly the array and work with it later)
3. How many web workers really bring an improvement?
4. Is there any advanced tutorial, besides the MDN-entry?
Thank you!
Best Answer
There's no problem with creating more than one worker, even if you don't keep track of them in an array. See below.
They can post a message back to you when they're done, with the results. Example below.
How long is a piece of string? :-) The answer will depend entirely on the target machine on which this is running. A lot of people these days have four or more cores on their machines. Of course, the machine is doing a lot of other things as well. You'll have to tune for your target environment.
There isn't a lot "advanced" about web workers. :-) I found this article was sufficient.
Here's an example running five workers and watching for them to be done:
Main window:
worker.js
:About the race condition that doesn't exist: If you think in terms of true pre-emptive threading, then you might think: I could create a worker, increment
running
to1
, and then before I create the next worker I could get the message from the first one that it's done, decrementrunning
to0
, and mistakenly think all the workers were done.That can't happen in the environment web workers work in. Although the environment is welcome to start the worker as soon as it likes, and a worker could well finish before the code starting the workers finished, all that would do is queue a call to the
workerDone
function for the main JavaScript thread. There is no pre-empting. And so we know that all workers have been started before the first call toworkerDone
is actually executed. Thus, whenrunning
is0
, we know they're all finished.Final note: In the above, I'm using
onmessage = ...
to hook up event handlers. Naturally that means I can only have one event handler on the object I'm doing that with. If you need to have multiple handlers for themessage
event, useaddEventListener
. All browsers that support web workers supportaddEventListener
on them (youdon't have to worry about the IEattachEvent
thing).