Question

var Worker = require('webworker-threads').Worker;
require('http').createServer(function (req,res) {
  var fibo = new Worker(function() {
    function fibo (n) {
      return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1;
    }

    // which onmessage does this this refer to?
    onmessage = function (event) {  //reference 1
      postMessage(fibo(event.data));
    }
  });
  fibo.onmessage = function (event) { //reference 2
    res.end('fib(40) = ' + event.data);
  };
  fibo.postMessage(40);
}).listen(port);

This is the code found as an example for the webworker class.

I was looking at the API and doen't seem to understand what reference 1 in the above code is referring to. Why does the postMessage(40) hit the inner onmessage function and not the fibo.onmessage function?

Était-ce utile?

La solution

The main point to note here is that the onmessage() and postmessage() is used as message bearers between both the main thread and the worker thread. This can be confusing initially. So the flow goes like this

  1. Create a worker thread .

    var fibo= new Worker...

This will spawn another JavaScript thread in the node. It can run in parallel in the background and use all the available CPU cores. Otherwise in node due to its single threaded model, taking advantage of multiple CPU cores is not possible (hence worker threads is a good approach for handling CPU-bound tasks in node)

  1. In the worker thread we define a)how to process the request/work it receives - the onmessage() does this job. It listens for any incoming work request and act on it.

onmessage= function (event) { //reference 1 postMessage(fibo(event.data)); }

b) how to communicate back to the main thread once work is done- postMessage does this job.

postMessage(fibo(event.data));
  1. In the main thread :- a. Call the worker thread and give it a task to execute -ie. using postmessage (By now you got the dance)

    fibo.postMessage(40);

b. Define listener regarding the action to take once the worker thread finishes it job and responds back. ie. using onmessage.

fibo.onmessage = function (event) { //reference 2
res.end('fib(40) = ' + event.data);

};

Autres conseils

try this code:

var port=8080;
var Worker = require('webworker-threads').Worker;
var fibo = new Worker(function() {
function fibo (n) {
    return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1;
}

// which onmessage does this this refer to?
onmessage = function (event) {  //reference 1
    console.log("fibo.onmessage inside");
    postMessage(fibo(event.data));
}
});
fibo.onmessage = function (event) { //reference 2
console.log("fibo.onmessage outside")
console.log('fib(40) = ' + event.data);
};
fibo.postMessage(40);

it gives

fibo.onmessage inside
fibo.onmessage outside
fib(40) = 165580141

The key thing is, it has to run on another node, so this implies that at some point, its doing (not even close but still applies)

    class worker {
        constructor(fnToRun){
            child_process.spawn('thread-creator-process', [
                '-fn', escape(fnToRun.toString())
            ]);
        }
    }

so now the other dude could do something like

    var result = new Function('context', 
    'with(context){'+
    '  ' + fnAsString + 
    '}'

result(thisThreadContext);

now context has the onmessage reference it will be created, and it can already have a reference to the postMessage and those things, with a little bit of parsing you have a new thread to work with

at least i think it that way

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