Node.JS中的Webworker线程 [英] Webworker-threads in Node.JS

查看:112
本文介绍了Node.JS中的Webworker线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#世界中的NodeJS新手,仍在尝试了解复杂的JavaScript迷宫. 我正在尝试通过 https://www.npmjs.com上的示例实现多线程/package/webworker-threads .

I am a NodeJS newbie from C# world still trying to understand the complex JavaScript maze. I am trying to implement the multi-threading from the example on https://www.npmjs.com/package/webworker-threads.

无法理解以下原因:

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

require('http').createServer(function (req,res) {
  console.log('request received.');
  var fibo = new Worker(function() {
    var calcFib = function (n) {
      return n > 1 ? calcFib(n - 1) + calcFib(n - 2) : 1;
    };
    this.onmessage = function (event) {
      postMessage(calcFib(event.data));
    };
  });
  fibo.onmessage = function (event) {
    var msg = 'fib(5) = ' + event.data;
    console.log(msg);
    res.end(msg);
  };
  fibo.postMessage(5);
}).listen(3000);

但是下面的只是不

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

//the below function just does not get called
var calcFib = function (n) {
    console.log('***This will not print. Can someone explain why?***');
    return n > 1 ? calcFib(n - 1) + calcFib(n - 2) : 1;
};

require('http').createServer(function (req,res) {
  console.log('request received.');
  var fibo = new Worker(function() {    
    this.onmessage = function (event) {
      postMessage(calcFib(event.data));
    };
  });
  fibo.onmessage = function (event) {
    var msg = 'fib(5) = ' + event.data;
    console.log(msg);
    res.end(msg);
  };
  fibo.postMessage(5);
}).listen(3000);

如果我排除calcFib的范围,为什么无法调用它.我的想法是在Webworker线程上实现包装器,该包装器可用于在另一个线程上执行任何CPU内部操作.但是,我什至无法调用外部函数

Why is it that if I take out the scope of calcFib, it cannot get called. My idea was to implement a wrapper over webworker-threads that could be used to perform any CPU intrinsic operations on another thread. However, I am not even able to call an external function

以下是对Benjamin Gruenbaum答案的答复:

如何在onmessage内部调用console.log,但不能在同一个文件的作用域之外定义我的函数?

How can I call console.log inside onmessage but not my function defined outside the scope in the same file?

this.onmessage = function (event) {
  console.log('can call console.log even if it is external');
  //cannot call calcFib(event.data) if it is defined outside the scope.
  postMessage(calcFib(event.data));
};

推荐答案

工作人员通过复制代码进行工作,因此所有关闭数据都将丢失.您无法从worker内访问闭包作用域,因此未定义calcFib.

Workers work by copying the code over so all closure data is lost. You cannot access closure scope from within the worker so calcFib is undefined.

您需要在工作代码中定义calcFib ,或通过消息将其发送并eval.

You need to define calcFib inside the worker code or send it via a message and eval it.

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

require('http').createServer(function (req,res) {
  console.log('request received.');

  var fibo = new Worker(function() {    
    //the below function just does not get called
    var calcFib = function (n) {
      console.log('***This will not print. Can someone explain why?***');
      return n > 1 ? calcFib(n - 1) + calcFib(n - 2) : 1;
    };
    this.onmessage = function (event) {
      postMessage(calcFib(event.data));
    };
  });
  fibo.onmessage = function (event) {
    var msg = 'fib(5) = ' + event.data;
    console.log(msg);
    res.end(msg);
  };
  fibo.postMessage(5);
}).listen(3000);

这篇关于Node.JS中的Webworker线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆