Webworker线程:是否可以使用"require"?内部工作者? [英] Webworker-threads: is it OK to use "require" inside worker?

查看:503
本文介绍了Webworker线程:是否可以使用"require"?内部工作者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(使用Sails.js)

(Using Sails.js)

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

I am testing webworker-threads ( https://www.npmjs.com/package/webworker-threads ) for long running processes on Node and the following example looks good:

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没有密码)

But as soon as I add any code to query Mongodb, it throws an exception: (not using the Sails model in the query, just to make sure the code could run on its own -- db has no password)

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之外也可以使用的方法,例如

Since the DB code works perfectly fine outside the Worker, I think it has something to do with the require. I've tried something that also works outside the Worker, like

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

并且代码还引发异常.不幸的是,console.log仅针对所有类型的错误显示此信息:

And the code also throws an exception. Unfortunately, console.log only shows this for all types of errors:

{Object}
{/Object}

因此,问题是:在Worker内使用require是否有任何限制或准则?我在这里怎么可能做错了?

So, the questions are: is there any restriction or guideline for using require inside a Worker? What could I be doing wrong here?

更新

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

it seems Threads will not allow external modules https://github.com/xk/node-threads-a-gogo/issues/22

TL:DR我认为,如果需要,则应使用节点的 集群或子进程.如果您想卸载一些CPU繁忙的工作, 您应该使用tagg和load函数来获取您的任何帮助者 需要.

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.

阅读此线程后,我发现此问题与此类似: 将Nodejs模块加载到Web Worker中

Upon reading this thread, I see that this question is similar to this one: Load Nodejs Module into A Web Worker

网络工作者线程作者奥德丽对此回答:

To which Audreyt, the webworker-threads author answered:

此处是webworker-threads的作者.感谢您使用该模块!

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

有一个默认的native_fs_对象,可以使用readFileSync 读取文件.

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

除此之外,我主要依靠onejs来编译所有必需的 package.json中的模块合并到单个JS文件中,以便importScripts能够 使用,就像部署到客户端Web Worker时所做的一样 环境. (onejs还有很多替代方法-browserify, 等)

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.)

希望这会有所帮助!

所以看来importScripts是要走的路.但是在这一点上,对于我想做的事情来说可能太过"hacky" ,因此KUE可能是一个更成熟的解决方案.

So it seems importScripts is the way to go. But at this point, it might be too hacky for what I want to do, so probably KUE is a more mature solution.

推荐答案

我是 node-webworker-threads 项目.

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

You are correct in your update: node-webworker-threads does not (currently) support requireing external modules.

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

It has limited support for some of the built-ins, including file system calls and a version of console.log. As you've found, the version of console.log implemented in node-webworker-threads is not identical to the built-in console.log in Node.js; it does not, for example, automatically make nice string representations of the components of an Object.

在某些情况下,您可以使用外部模块,如奥德丽特在回应中概述的那样.显然,这并不理想,我认为不完整的requirenode-webworker-threads的主要破坏者".我希望今年夏天能继续努力.

In some cases you can use external modules, as outlined by audreyt in her response. Clearly this is not ideal, and I view the incomplete require as the primary "dealbreaker" of node-webworker-threads. I'm hoping to work on it this summer.

node-webworker-threads允许您根据WebWorker API进行编码,并在客户端(浏览器)和服务器(Node.js)中运行相同的代码.这就是为什么您会在 node-threads-a-gogo 上使用node-webworker-threads的原因

node-webworker-threads allows you to code against the WebWorker API and run the same code in the client (browser) and the server (Node.js). This is why you would use node-webworker-threads over node-threads-a-gogo.

node-webworker-threads就是很好的选择.例如:质数,斐波那契,蒙特卡洛模拟,卸载内置但可能昂贵的运算(如正则表达式匹配).

node-webworker-threads is great if you want the most lightweight possible JavaScript-based workers, to do something CPU-bound. Examples: prime numbers, Fibonacci, a Monte Carlo simulation, offloading built-in but potentially-expensive operations like regular expression matching.

node-webworker-threads强调便携性而不是便利性.对于仅使用Node.js的解决方案,这意味着node-webworker-threads并非可行之路.

node-webworker-threads emphasizes portability over convenience. For a Node.js-only solution, this means that node-webworker-threads is not the way to go.

如果您愿意在全栈可移植性上做出妥协,则有两种方法可供选择:速度和便利性.

If you're willing to compromise on full-stack portability, there are two ways to go: speed and convenience.

对于速度,请尝试使用 C ++插件.使用 NaN .我建议使用Scott Frees的 C ++和Node.js集成书来了解如何进行此操作,它将为您节省很多时间很多时间.您需要为精通C ++技能而为此付出代价,如果您想与MongoDB一起工作,那么这可能不是一个好主意.

For speed, try a C++ add-on. Use NaN. I recommend Scott Frees's C++ and Node.js Integration book to learn how to do this, it'll save you a lot of time. You'll pay for it in needing to brush up on your C++ skills, and if you want to work with MongoDB then this probably isn't a good idea.

为方便起见,请使用基于子进程的工作程序池例如叉架.在这种情况下,每个工作程序都是成熟的Node.js实例.然后,您可以require尽其所能.与node-webworker-threads或C ++附加组件相比,您将为此付出更多的应用空间并支付更高的通信成本.

For convenience, use a Child Process-based worker pool like fork-pool. In this case, each worker is a full-fledged Node.js instance. You can then require to your heart's content. You'll pay for it in a larger application footprint and in higher communication costs compared to node-webworker-threads or a C++ add-on.

这篇关于Webworker线程:是否可以使用"require"?内部工作者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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