Git Product home page Git Product logo

Comments (7)

Byte-Lab avatar Byte-Lab commented on September 14, 2024 4

So just to be clear, even if we use browserify to package required modules into something that can be consumed by importScripts, it won't work -- it seems like we can't use modules inside of web workers.

Take the following code:

// example_mod.js
var my_mod = require('build/Release/my_module');

and say we run browserify example_mod.js > example_import.js
If I then have the following:

var mod_work = new Threads.Worker(function() {
    function performWork() {
        importScripts('example_import.js');
        return my_mod.run();
    }

   this.onmessage = function(event) {
        postMessage(performWork());
   }
});

mod_work.postMessage();

Then the call to my_mod.run() hangs indefinitely without being called.

Am I misunderstanding how to use web workers, or can we really not use modules inside of thread workers even if we wrap their code in something that can be called by importScripts?

from node-webworker-threads.

the1mills avatar the1mills commented on September 14, 2024 4

for the love of all things holy can someone just create "require"
functionality in webworkers?
On Jun 5, 2016 8:18 AM, "唐鳳" [email protected] wrote:

Hmm, this looks legitimate and should work for pure-javascript modules. If
I set example_import.js to:

var my_mod = {run: function(){ return 123 }}

and change the postMessage(performWork()); line to:

   console.log(performWork());

Then I do find it printed out. However if build/Release/my_module is a
binary module (built by node-gyp or similar) then indeed, we have no way
of loading them currently.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#15 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/ACDIYCPUsdYwvTosm10ercV98LrVNtorks5qIujHgaJpZM4Aqh3K
.

from node-webworker-threads.

audreyt avatar audreyt commented on September 14, 2024

Hi, currently as per Web Worker spec, only importScripts is supported.

To package node modules into .js files suitable for importScripts, perhaps give https://github.com/azer/onejs a try?

from node-webworker-threads.

arunkjn avatar arunkjn commented on September 14, 2024

@audreyt a small example or snippet would be appreciated.

from node-webworker-threads.

audreyt avatar audreyt commented on September 14, 2024

Hmm, this looks legitimate and should work for pure-javascript modules. If I set example_import.js to:

var my_mod = {run: function(){ return 123 }}

and change the postMessage(performWork()); line to:

       console.log(performWork());

Then I do find it printed out. However if build/Release/my_module is a binary module (built by node-gyp or similar) then indeed, we have no way of loading them currently.

from node-webworker-threads.

the1mills avatar the1mills commented on September 14, 2024

I dont have the expertise to do this but audrey you probably do or someone
around here does. it would be so frickin' useful to have require abilities
in webworkers.
On Jun 5, 2016 10:38 AM, [email protected] wrote:

for the love of all things holy can someone just create "require"
functionality in webworkers?
On Jun 5, 2016 8:18 AM, "唐鳳" [email protected] wrote:

Hmm, this looks legitimate and should work for pure-javascript modules.
If I set example_import.js to:

var my_mod = {run: function(){ return 123 }}

and change the postMessage(performWork()); line to:

   console.log(performWork());

Then I do find it printed out. However if build/Release/my_module is a
binary module (built by node-gyp or similar) then indeed, we have no way
of loading them currently.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#15 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/ACDIYCPUsdYwvTosm10ercV98LrVNtorks5qIujHgaJpZM4Aqh3K
.

from node-webworker-threads.

audreyt avatar audreyt commented on September 14, 2024

@the1mills Please consider using https://www.npmjs.com/package/tiny-worker which does support require through child processes.

from node-webworker-threads.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.