最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a way to set a Web Worker to low priority? - Stack Overflow

programmeradmin2浏览0评论

I am thinking of using Web Workers to provide some background functionality while a user is browsing my website (that's what Web Workers are for, right?). However, I don't want to take the risk of compromising the user experience by causing laggy scrolling, unresponsive controls, etc. Web Workers are mapped on OS threads, hence I would expect some control on the priority of these threads however, as far as I know, there's no such thing in the current API. Do you know a how to accomplish this? Even with a hack?

I am thinking of using Web Workers to provide some background functionality while a user is browsing my website (that's what Web Workers are for, right?). However, I don't want to take the risk of compromising the user experience by causing laggy scrolling, unresponsive controls, etc. Web Workers are mapped on OS threads, hence I would expect some control on the priority of these threads however, as far as I know, there's no such thing in the current API. Do you know a how to accomplish this? Even with a hack?

Share Improve this question edited May 2, 2016 at 13:20 Mogsdad 45.7k21 gold badges162 silver badges285 bronze badges asked Dec 29, 2011 at 14:44 tunnuztunnuz 24k33 gold badges95 silver badges135 bronze badges 7
  • 2 On what platform and language? – rene Commented Jan 1, 2012 at 17:41
  • What kind of task do you intend to use the web workers for? Periodic activity? One-time execution of a task? – Tudor Commented Jan 1, 2012 at 18:31
  • @Tudor I would say one-time execution (e.g. the user uploads a photograph and the worker applies a Photoshop-like filter to it, which is pretty CPU intensive, then the worker alerts the main thread). – tunnuz Commented Jan 2, 2012 at 7:46
  • 3 @rene in JS, and platform independently. – tunnuz Commented Jan 2, 2012 at 7:46
  • You should ready the comments on this page (some other users experiences with trying to manipulate pixel data with a Web Worker). You may want to try to go with a server-side solution and an ajax request to pass the image back and forth to the filter. I am not sure what is a greater sacrifice, bandwidth or cpu usage. Also some reading on mozilla.org mention web workers always running in a low priority under the higher UI layer thread. nooshu.com/mandelbrot-renderer-update – Mike L. Commented Jan 3, 2012 at 23:40
 |  Show 2 more comments

2 Answers 2

Reset to default 11 +50

Well, there's no API call to control low-level details like this. However, I think you should first implement what you want to do and then test if the performance hit is too great on the user experience. I'm assuming that since they did not add fine control over how the threads execute, they're probably well managed by the underlying implementation.

Even with a hack? [...] the user uploads a photograph and the worker applies a Photoshop-like filter to it, which is pretty CPU intensive, then the worker alerts the main thread

Here's a hack.

Slow down your code. Something like this is what I am currently using for a particle simulation:

var TIME_STEP = 10,
    paused = false,
    state; // set by commands.start()

function main_loop () {
    if (paused) {
        return;
    }

    // update state incrementally. Break your process into chunks
    // for example pixels or rows of pixels
    state = ____________;

    // send state or progress to main thread
    if (finished) {
        self.postMessage(state);
    } else {
        self.postMessage(progress);
    }

    setTimeout(main_loop, TIME_STEP);
}

var commands = {
    //...functions that can be called from main thread (pause/start/cancel/etc)...
};

function message_handler (event) {
    var data = event.data;
    var command = commands[data.command];

    command.apply(this, data.args);
}

self.addEventListener('message', message_handler, false);

TIME_STEP is the time between calculations and will need to be different depending what you are doing and how long you can afford to increase it's time. One good thing about doing it this way is that you can accept pause and cancel requests between iterations.

发布评论

评论列表(0)

  1. 暂无评论