Why can't Web Worker call a function directly?

前端 未结 7 1040
借酒劲吻你
借酒劲吻你 2021-02-01 03:15

We can use the web worker in HTML5 like this:

var worker = new Worker(\'worker.js\');

but why can\'t we call a function like this?



        
相关标签:
7条回答
  • 2021-02-01 03:27

    By design web workers are multi-threaded, javascript is single threaded"*"multiple scripts cannot run at the same time.

    refer to: http://www.html5rocks.com/en/tutorials/workers/basics/

    0 讨论(0)
  • 2021-02-01 03:27

    Just use my tiny plugin https://github.com/zevero/worker-create

    and do

    var worker_url = Worker.create(function(e){
      self.postMessage('Example post from Worker'); //your code here
    });
    var worker = new Worker(worker_url);
    
    0 讨论(0)
  • 2021-02-01 03:31

    WebWorkers Essentials

    WebWorkers are executed in an independent thread, so have no access to the main thread, where you declare them (and viceversa). The resulting scope is isolated, and restricted. That's why, you can't , for example, reach the DOM from inside the worker.


    Communication with WebWorkers

    Because communication betwen threads is neccessary, there are mechanisms to accomplish it. The standard communication mechanism is through messages, using the worker.postMessage() function and the worker.onMessage(), event handler.

    More advanced techniques are available, involving sharedArrayBuffers, but is not my objective to cover them. If you are interested in them, read here.


    Threaded Functions

    That's what the standard brings us. However, ES6 provides us enough tools, to implement an on-demmand callable Threaded-Function.

    Since you can build a Worker from a Blob, and your Function can be converted into it (using URL.createObjectURL), you only need to implement some kind of Communication Layer in both threads, to handle the messages for you, and obtain a natural interaction.

    Promises of course, are your friend, considering that everything will happen asynchronously.

    Applying this theory, you can implement easilly, the scenario you describe.


    My personal approach : ParallelFunction

    I've recently implemented and publised a tiny library wich does exactly what you describe. in less than 2KB (minified).

    It's called ParallelFunction, and it's available in github, npm , and a couple of CDNs.

    As you can see, it totally matches your request:

    // Your function...
    let calculatePi = new ParallelFunction( function(n){
        // n determines the precision , and in consequence 
        // the computing time to complete      
        var v = 0;
        for(let i=1; i<=n; i+=4) v += ( 1/i ) - ( 1/(i+2) );
        return 4*v;
    });
    
    // Your async call...
    calculatePi(1000000).then( r=> console.log(r) );
    
    // if you are inside an async function you can use await...
    ( async function(){    
        let result = await calculatePi(1000000);
        console.log( result );
    })()
    
    // once you are done with it...
    calculatePi.destroy();
    

    After initialization, you can call your function as many times you need. a Promise will be returned, wich will resolve, when your function finishes execution.

    By the way, many other Libraries exists.

    0 讨论(0)
  • 2021-02-01 03:35

    While it's not optimal and it's been mentioned in the comments, an external file is not needed if your browser supports blobURLs for Web Workers. HTML5Rocks was the inspiration for my code:

    function sample(e)
    {
        postMessage(sample_dependency());
    }
    
    function sample_dependency()
    {
        return "BlobURLs rock!";
    }
    
    var blob = new Blob(["onmessage = " + sample + "\n" + sample_dependency]);
    var blobURL = window.URL.createObjectURL(blob);
    var worker = new Worker(blobURL);
    
    worker.onmessage = function(e)
    {
        console.log(e.data);
    };
    
    worker.postMessage("");
    

    Caveats:

    • The blob workers will not successfully use relative URLs. HTML5Rocks link covers this but it was not part of the original question.

    • People have reported problems using Blob URLs with Web Workers. I've tried it with IE11 (whatever shipped with FCU), MS Edge 41.16299 (Fall Creator's Update), Firefox 57, and Chrome 62. No clue as to Safari support. The ones I've tested have worked.

    • Note that "sample" and "sample_dependency" references in the Blob constructor call implicitly call Function.prototype.toString() as sample.toString() and sample_dependency.toString(), which is very different than calling toString(sample) and toString(sample_dependency).

    Posted this because it's the first stackoverflow that came up when searching for how to use Web Workers without requesting an additional file.

    Took a look at Zevero's answer and the code in his repo appears similar. If you prefer a clean wrapper, this is approximately what his code does.

    Lastly -- I'm a noob here so any/all corrections are appreciated.

    0 讨论(0)
  • 2021-02-01 03:41

    This question has been asked before, but for some reason, the OP decided to delete it.
    I repost my answer, in case one needs a method to create a Web worker from a function.


    In this post, three ways were shown to create a Web worker from an arbitrary string. In this answer, I'm using the third method, since it's supported in all environments.

    A helper file is needed:

    // Worker-helper.js
    self.onmessage = function(e) {
        self.onmessage = null; // Clean-up
        eval(e.data);
    };
    

    In your actual Worker, this helper file is used as follows:

    // Create a Web Worker from a function, which fully runs in the scope of a new
    //    Worker
    function spawnWorker(func) {
        // Stringify the code. Example:  (function(){/*logic*/}).call(self);
        var code = '(' + func + ').call(self);';
        var worker = new Worker('Worker-helper.js');
        // Initialise worker
        worker.postMessage(code);
        return worker;
    }
    
    var worker = spawnWorker(function() {
        // This function runs in the context of a separate Worker
        self.onmessage = function(e) {
            // Example: Throw any messages back
            self.postMessage(e.data);
        };
        // etc..
    });
    worker.onmessage = function() {
        // logic ...
    };
    worker.postMessage('Example');
    

    Note that the scopes are strictly separated. Variables can only be passed and forth using worker.postMessage and worker.onmessage. All messages are structured clones.

    0 讨论(0)
  • 2021-02-01 03:42

    This answer might be a bit late, but I wrote a library to simplify the usage of web workers and it might suit OP's need. Check it out: https://github.com/derekchiang/simple-worker

    It allows you to do something like:

    SimpleWorker.run({
      func: intensiveFunction,
      args: [123456],
      success: function(res) {
        // do whatever you want
      },
      error: function(err) {
        // do whatever you want
      }
    })
    
    0 讨论(0)
提交回复
热议问题