audreyt / node-webworker-threads

Lightweight Web Worker API implementation with native threads
https://npmjs.org/package/webworker-threads
Other
2.3k stars 149 forks source link

require not working inside a thread #168

Open weekens opened 7 years ago

weekens commented 7 years ago

For the moment, I cannot require modules from inside a thread. The following code would print an error:

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

var worker = new Worker(function() {
  try {
    console.log('>>> In worker, require = ' + require);
  }
  catch (e) {
    console.log('Error: ' + e);
  }
});
Error: ReferenceError: require is not defined
EmaX093 commented 7 years ago

From readme: "Note: If you would like to require() native modules in a worker, please consider using the process-based tiny-worker instead. It does not use threads, but the WebWorker API is compatible."

weekens commented 7 years ago

Why cannot require() be implemented in threads? Is this a fundamental limitation? Could you explain?

konradjurk commented 7 years ago

@Reltik native modules

devsnek commented 7 years ago

@weekens require calls through node's native codebase, which is very much not thread safe, and would end up breaking things pretty badly.

weekens commented 7 years ago

@devsnek But maybe it is possible to polyfill require call inside thread code and implement some kind of a global "require lock" to avoid callling require from several threads simultaneously?

wmertens commented 6 years ago

@weekens no, that's not possible. v8 is not thread safe.

devsnek commented 6 years ago

even if you could somehow manage to call require (which v8::Isolate will not allow under any circumstance), then getting the resulting value from that back into your thread would be doubly impossible (again v8::Isolate will not allow it)

TheL1ne commented 6 years ago

additionally normal importing is not part of the WebWorker API specification and therefore I doubt this will be changed. you can use ImportScripts() for your own scripts.

taoqf commented 6 years ago

If require could be passed into threads as arguments, that would do.

taoqf commented 6 years ago

like this:

const id = new Function('args', 'require', 'const uuid = require("uuid");return uuid();')({}, require);