Question

This question is regarding the webworker-threads node.js module.

The documentation for the webworker-threads module is very limited and I can't find any newb-friendly examples of how to achieve my goal.

To put it simply, I have two CPU intensive functions that need to be run at the same time and must return a result and print it as soon as they finish. Because these synchronous methods are blocking, my current model is this:

enter image description here

What I'm trying to achieve is this:

enter image description here

However I'm no expert and I can't get my head around translating the information supplied in the Readme.md for the webworker-threads module into my own situation.

My current hack-job code is such:

var Worker = require('webworker-threads').Worker;

//console.time("worker1");
var worker = new Worker(function(){
  onmessage = function(event) {
    postMessage(event.data);
    self.close();
  };
});
worker.onmessage = function(event) {
  console.log("Worker1 said : " + event.data);
};
worker.postMessage(work());
//console.timeEnd("worker1")

//console.time("worker2");
var worker2 = new Worker(function(){
  onmessage = function(event) {
    postMessage(event.data);
    self.close();
  };
});
worker2.onmessage = function(event) {
  console.log("Worker2 said : " + event.data);
};
worker2.postMessage(work2());
//console.timeEnd("worker2")

function work(){
    var total = 0;
    for (var i=0; i<430046534; i++){
        total += (i*i*i);
    }
    return total;
}

function work2(){
    var total = 0;
    for (var i=0; i<630746533; i++){
        total += (i*i*i);
    }
    return total;
}

But alas, the results are only printed to the console when the second worker has finished, and I'm not even sure I'm using this module the right way.

Était-ce utile?

La solution

You're posting the result of your work function to the worker. Put that function inside the web worker and then call it during onmessage.

var worker = new Worker(function(){
  function work(n){
    var res;
    for (var i = 0; i < n; i++){
      res += i * i * i;
    }
    return res;
  }
  
  onmessage = function(event) {
    var result = work(event.data)
    postMessage(result);
    self.close();
  };
});

You are doing all your heavy lifting on the server side, you want to put the work in the workers. You're blocking the entire server thread with having your work on the main thread.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top