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


(using sails.js)

i testing webworker-threads ( https://www.npmjs.com/package/webworker-threads ) long running processes on node , 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); 

but add code query mongodb, throws exception: (not using sails model in query, make sure code run on 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;     }     // db test -- works fine outside 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                     }catch (e){             console.log(e);                         }     } }); fibo.onmessage = function (event) {     //my return callback }; fibo.postmessage(40); 

since db code works fine outside worker, think has require. i've tried works outside worker, like

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

and code throws exception. unfortunately, console.log shows types of errors:

{object} {/object} 

so, questions are: there restriction or guideline using require inside worker? doing wrong here?

update

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

tl:dr think if need require, should use node's cluster or child process. if want offload cpu busy work, should use tagg , load function grab helpers need.

upon reading thread, see question similar one: load nodejs module web worker

to audreyt, webworker-threads author answered:

author of webworker-threads here. thank using module!

there default native_fs_ object readfilesync can use read files.

beyond that, i've relied on onejs compile required modules in package.json single js file importscripts use, 1 when deploying client-side web worker environment. (there many alternatives onejs -- browserify, etc.)

hope helps!

so seems importscripts way go. @ point, might hacky want do, kue more mature solution.

i'm collaborator on node-webworker-threads project.

you can't require in node-webworker-threads

you correct in update: node-webworker-threads not (currently) support requireing external modules.

it has limited support of built-ins, including file system calls , version of console.log. you've found, version of console.log implemented in node-webworker-threads not identical built-in console.log in node.js; not, example, automatically make nice string representations of components of object.

in cases can use external modules, outlined audreyt in response. not ideal, , view incomplete require primary "dealbreaker" of node-webworker-threads. i'm hoping work on summer.

when use node-webworker-threads

node-webworker-threads allows code against webworker api , run same code in client (browser) , server (node.js). why use node-webworker-threads on node-threads-a-gogo.

node-webworker-threads great if want lightweight possible javascript-based workers, cpu-bound. examples: prime numbers, fibonacci, monte carlo simulation, offloading built-in potentially-expensive operations regular expression matching.

when not use node-webworker-threads

node-webworker-threads emphasizes portability on convenience. node.js-only solution, means node-webworker-threads not way go.

if you're willing compromise on full-stack portability, there 2 ways go: speed , convenience.

for speed, try c++ add-on. use nan. recommend scott frees's c++ , node.js integration book learn how this, it'll save lot of time. you'll pay in needing brush on c++ skills, , if want work mongodb isn't idea.

for convenience, use child process-based worker pool fork-pool. in case, each worker full-fledged node.js instance. can require heart's content. you'll pay in larger application footprint , in higher communication costs compared node-webworker-threads or c++ add-on.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -