用于Node.js中CPU密集型功能的Web Workers vs child_process

时间:2017-01-25 04:15:04

标签: javascript node.js web-worker

我正在尝试使用node-unfluff,它从HTML字符串中提取内容。但是,它通常需要大约200毫秒才能运行。由于它同步运行,这太慢了。我想让它以异步方式运行。

据我所知,我的选择是Web Workers(https://github.com/audreyt/node-webworker-threads)或child_processhttps://nodejs.org/api/child_process.html)。还有其他更好的选择吗?

如果没有,哪个在速度或其他因素方面更好?

修改

还有Threadsàgogo(https://github.com/xk/node-threads-a-gogo)和small-worker(https://github.com/avoidwork/tiny-worker)。

WebWorker Threads不支持require,因此不再是一种选择。

使用require函数使用Threadsàgogo可以load个文件,但这似乎是一个hacky解决方法。

微小工人目前在Github只有26颗星,所以我对在生产代码中使用它犹豫不决。它支持require

如果没有更好的选择,我正在考虑使用child_process编写自己的WebWorker实现。

1 个答案:

答案 0 :(得分:1)

您可以将require与Workers一起使用。在您的工作人员脚本中,您需要调用

self.importScripts('../path/require.js');

根据require文档,您可以将配置对象传递给模块:

requirejs.config({
    //By default load any module IDs from js/lib
    baseUrl: 'js/lib',
    //except, if the module ID starts with "app",
    //load it from the js/app directory. paths
    //config is relative to the baseUrl, and
    //never includes a ".js" extension since
    //the paths config could be for a directory.
    paths: {
        app: '../app'
    }
});

// Start the main app logic.
requirejs(['jquery', 'canvas', 'app/sub'],
function   ($,        canvas,   sub) {
    //jQuery, canvas and the app/sub module are all
    //loaded and can be used here now.
});

将它们放在一起

<强> Worker.js

self.importScripts('../path/require.js');
requirejs.config({
    //By default load any module IDs from path/lib
    baseUrl: 'path/lib',
    //except, if the module ID starts with "app",
    //load it from the js/app directory. paths
    //config is relative to the baseUrl, and
    //never includes a ".js" extension since
    //the paths config could be for a directory.
    paths: {
        app: '../app'
    }
});

// Start the main app logic.
requirejs(['jquery', 'canvas', 'app/sub'],
function   ($,        canvas,   sub) {
    //jQuery, canvas and the app/sub module are all
    //loaded and can be used here now.
    // now you can post a message back to your callee script to let it know require has loaded
    self.postMessage("initialized");
});

self.onmessage = function(message) {
    // do cpu intensive work here, this example is not cpu intensive...
    if(message.data === 'to process') {
        self.postMessage("completed!");
    }
}

节点工作者呼叫

var worker = new Worker('Worker.js');
worker.onmessage = function(event) {
    var msg = event.data;
    if(msg === 'initialized') {
         worker.postMessage({data: 'to process'});
    }
}