javascript - How to wait for multiple WebWorkers in a loop -


i have following issue web workers in js. have heavy duty application doing simulation. code runs in multiple web workers. main thread running on webpage. web worker, if makes sense.

example:

    var mywebworkers = [];     function openworker(workercount){         for(var = 0; < workercount; i++){             mywebworkers[i] = new worker('worker.js');             mywebworkers[i].onmessage = function(e){                 this.result = e.data;                 this.isready = true;             }         }     }      function setworkerdata(somedata){         // somedata.length multiple of mywebworkers.length         var elementcntperworker = somedata.length / mywebworkers.length;         mywebworkers.foreach(function(worker, index){             worker.isready = false;             worker.postmessage(                 somedata.slice(index * elementcntperworker,                      (index + 1) * elementcntperworker - 1));         });     }      var somedata = [...];     openworker(8);      for(var = 0; < 10000; i++){          setworkerdata(somedata);          waituntilworkersaredonebutallowbrowsertoreact(mywebworkers);          if(x % 100) updatesvgonwebpage     }      function waituntilworkersaredonebutallowbrowsertoreact(){         /* wait mywebworkers-onchange event,            allow browser react , don't block full web worker             following example intension. not work, because             events not executed until code excution stops.         */         somedata = [];         for(var = 0; < mywebworkers.length; i++){             while(!mywebworkers[i].isready);             somedata = somedata.concat(mywebworkers.result);         }     } 

what need waituntilworkersaredonebutallowbrowsertoreact function or concept running. every searching reagarding mutex, sleep, etc ends in following sentences: "js single threaded", "this work if not in loop", "there no reason have sleep function". etc.

even when passing main task worker, got problem, thread 100 % duty on checking, if others ready, waste of energy , processing power.

i love have blocking function mywebworker.waitforready(), allow events still handled. bring javascript next level. may missed simple concept this.

thank you!

i love have blocking function mywebworker.waitforready()

no, that's not possible. statements researched correct, web workers stay asynchronous , communicate messages. there no waiting events, not on worker threads.

you want use promises this:

function createworkers(workercount, src) {     var workers = new array(workercount);     (var = 0; < workercount; i++) {         workers[i] = new worker(src);     }     return workers; } function dowork(worker, data) {     return new promise(function(resolve, reject) {         worker.onmessage = resolve;         worker.postmessage(data);     }); } function dodistributedwork(workers, data) {     // data size multiple of number of workers     var elementsperworker = data.length / workers.length;     return promise.all(workers.map(function(worker, index) {         var start = index * elementsperworker;         return dowork(worker, data.slice(start, start+elementsperworker));     })); }  var mywebworkers = createworkers(8, 'worker.js'); var somedata = [...]; function step(i) {     if (i <= 0)         return promise.resolve("done!");     return dodistributedwork(mywebworkers, somedata)     .then(function(results) {         if (i % 100)             updatesvgonwebpage();         return step(i-1)     }); } step(1000).then(console.log); 

promise.all magic of waiting concurrently running results, , step function asynchronous looping using recursive approach.


Comments