question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pass arguents to worker

See original GitHub issue

Hi! 😃

I need to pass simple arguments to a worker. Somewhat like that:

max = 42;

var w = new Worker(function () {
    for (var i = 0; i < max; i++) {
        // Do something...
    }
}

max is in a different context than what is accessible from within the worker. How can I pass its value (or alternatively, the main context) to the worker?

The only way I would see is to use w.postMessage(max) after creating w, and have the thread inside of w block until it receives this first message. But that seems rather inelegant.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
BlessYAHUcommented, Aug 9, 2018

You’re right. That doesn’t work with workers. You will probably need to pass in the argument with a post message (I verified this worked by running it):

var Worker = require("tiny-worker");

var x = 3;
var fun = function (max) {
    let a = max;
    let maxi = a;
    for (var i = 0; i < maxi; i++) {
        console.log(i)
    }
};

var w = new Worker(function() {
    self.onmessage = function (ev) {
        postMessage(ev.data);
    }
});

w.onmessage = function(ev) {
    fun(ev.data);
    w.terminate();
}

w.postMessage(x);
0reactions
avoidworkcommented, Aug 11, 2018

feel free to start a new issue, someone might be able to help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript web workers - how do I pass arguments?
How do I pass an argument to a web worker though using this basic example? contents of worker.js: function doSomething() { postMessage( '...
Read more >
How can you pass additional args to a worker function?
How to pass the additional args and kwargs in execute_this_fn below is ... worker = Worker(lambda :self.execute_this_fn(arg1, arg2, kwarg1).
Read more >
How to pass function to Web Workers - Localazy
The most scalable option I've found is to have two stable parameters, one for functions and one for their arguments a simply send...
Read more >
Passing ARGS to multiple workers - Julia Discourse
I am trying to pass an argument from the terminal to a program that uses multiple cores. But ARGS appear to be visible...
Read more >
Passing arguments from command line ( or worker.main() ) all ...
I am currently looking at a way to pass a parameter from the command line ( or worker.main() ) to the task (...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found