Webworker-threads:可以在 worker 中使用 "require" 吗?

Webworker-threads: is it OK to use "require" inside worker?

(使用 Sails.js)

我正在测试 webworker-threads ( https://www.npmjs.com/package/webworker-threads ) 在 Node 上的长时间 运行ning 进程,下面的例子看起来不错:

var Worker = require('webworker-threads').Worker;
var fibo = new Worker(function() {
    function fibo (n) {
        return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1;
    }
    this.onmessage = function (event) {
        try{
            postMessage(fibo(event.data));                

        }catch (e){
            console.log(e);                
        }
    }
});
fibo.onmessage = function (event) {
    //my return callback
};
fibo.postMessage(40);

但是只要我添加任何代码来查询 Mongodb,它就会抛出异常: (不在查询中使用 Sails 模型,只是为了确保代码可以 运行 自己 -- db 没有密码)

var Worker = require('webworker-threads').Worker;
var fibo = new Worker(function() {
    function fibo (n) {
        return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1;
    }
    // MY DB TEST -- THIS WORKS FINE OUTSIDE THE WORKER
    function callDb(event){
        var db = require('monk')('localhost/mydb');
        var users = db.get('users');
        users.find({ "firstName" : "John"}, function (err, docs){
            console.log(("serviceSuccess"));
            return fibo(event.data);
        });
    }
    this.onmessage = function (event) {
        try{

            postMessage(callDb(event.data));     // calling db function now           

        }catch (e){
            console.log(e);                
        }
    }
});
fibo.onmessage = function (event) {
    //my return callback
};
fibo.postMessage(40);

由于 DB 代码在 Worker 之外工作得很好,我认为它与 require 有关。我尝试了一些在 Worker 之外也可以工作的东西,比如

 var moment = require("moment");
 var deadline = moment().add(30, "s");

并且代码也抛出异常。不幸的是,console.log 只显示所有类型的错误:

{Object}
{/Object}

所以,问题是:在 Worker 中使用 require 是否有任何限制或指南?我在这里做错了什么?

更新

Threads 似乎不允许外部模块 https://github.com/xk/node-threads-a-gogo/issues/22

TL:DR I think that if you need to require, you should use a node's cluster or child process. If you want to offload some cpu busy work, you should use tagg and the load function to grab any helpers you need.

读完这个帖子,我发现这个问题和这个问题很相似: Load Nodejs Module into A Web Worker

webworker-threads 作者 Audreyt 对此做出了回答:

author of webworker-threads here. Thank you for using the module!

There is a default native_fs_ object with the readFileSync you can use to read files.

Beyond that, I've mostly relied on onejs to compile all required modules in package.json into a single JS file for importScripts to use, just like one would do when deploying to a client-side web worker environment. (There are also many alternatives to onejs -- browserify, etc.)

Hope this helps!

看来 importScripts 是正确的选择。但在这一点上,对于我想做的事情来说,它可能太 hacky,所以可能 KUE 是一个更成熟的解决方案。

我是 node-webworker-threads 项目的合作者。

你不能 requirenode-webworker-threads

您的更新是正确的:node-webworker-threads(当前)不支持 requireing 外部模块。

它对某些 built-ins 的支持有限,包括文件系统调用和 console.log 的一个版本。如您所见,node-webworker-threads 中实现的 console.log 版本与 Node.js 中的 built-in console.log 不同;例如,它不会自动对对象的组件进行漂亮的字符串表示。

在某些情况下,您可以使用外部模块,正如 audreyt 在她的回复中所概述的那样。显然这并不理想,我将不完整的 require 视为 node-webworker-threads 的主要 "dealbreaker"。我希望今年夏天能继续努力。

何时使用node-webworker-threads

node-webworker-threads 允许您在客户端(浏览器)和服务器(Node.js)中针对 WebWorker API 和 运行 编写相同的代码。这就是为什么您会使用 node-webworker-threads 而不是 node-threads-a-gogo

node-webworker-threads 如果您想要尽可能轻量级的 JavaScript-based 工人来做某事 CPU-bound,那是很棒的选择。示例:质数、斐波那契、Monte Carlo 模拟、卸载 built-in 但 potentially-expensive 正则表达式匹配等操作。

使用node-webworker-threads

node-webworker-threads 强调便携性而非便利性。对于 Node.js-only 解决方案,这意味着 node-webworker-threads 不是可行的方法。

如果您愿意在 full-stack 便携性上做出妥协,有两种选择:速度和便利性。

为了 speed,尝试阅读 C++ add-on. Use NaN. I recommend Scott Frees's C++ and Node.js Integration 一本书来学习如何做到这一点,它会为您节省很多时间。你会因为需要温习你的 C++ 技能而付出代价,如果你想使用 MongoDB 那么这可能不是一个好主意。

为了方便,使用Child Process-based worker pool like fork-pool。在这种情况下,每个工人都是一个 full-fledged Node.js 实例。然后,您可以 require 尽情享受。与 node-webworker-threads 或 C++ add-on.

相比,您需要为它支付更大的应用程序占用空间和更高的通信成本