Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts

    Unhappy Trying to learn webWorker

    Hello, everyone.

    Just playing around with webWorker, trying to learn about it. One question:

    Is it possible for a Worker to spawn another Worker?

    I'm trying to spawn a Worker from within another Worker, and I'm getting the error message that the script I'm trying to open from within the first Worker can't be found.

    Thank you,
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,333
    Thanks
    11
    Thanked 587 Times in 568 Posts
    no, Worker is not in the worker global.
    you can use a shared worker, which might do what you want.

    one way to do what you asked is to have the worker send a message to the page that in turn spawns a new worker.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #3
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    That's a thought. But the reason I ask is because I tried spawning two Workers from the same .js page, thinking that each would spawn in its own thread, but it looks as though they both spawned in the same thread. I assumed that if a Worker spawned another Worker that they would each use a different thread. Just throwing things against the wall, seeing which sticks.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,333
    Thanks
    11
    Thanked 587 Times in 568 Posts
    Quote Originally Posted by WolfShade View Post
    That's a thought. But the reason I ask is because I tried spawning two Workers from the same .js page, thinking that each would spawn in its own thread, but it looks as though they both spawned in the same thread.
    in chrome in particular, just like in different tabs, sometimes a couple can share a single process. How that break down to the thread level is actually inconsequential in modern OSes due to thread scheduling.

    The big advantage to worker threads is that they won't freeze the browser chrome:


    Code:
    function execute(
     //args:
     fnWorkerCode,//  code to be executed by worker (no scope/win/dom).
     fnCallBack,  // takes processed result returned by worker's postMessage() call
     boolRepeatable // if not expecting more than one result, kill the worker.
    
        ){//function body:
        var worker=fnWorkerCode,
        url="http://danml.com/got/?type=text/javascript&data="+
        encodeURIComponent((worker+"").slice( (""+worker).split(/{/)[0].length+1,-1).trim()),
    
    
        myWorker = new Worker(url);
        myWorker.onmessage = function(event) {
          fnCallBack(event.data);
          if(!boolRepeatable){myWorker.terminate();}
        };
        myWorker.onerror = function(event) {myWorker.terminate();}
      return function(data){  myWorker.postMessage(data); }
    }
    
    
    
    
    /*
     //example:
    
    
    //code to run in new thread (won't stall browser tab ui)
    function worker_fib(){
      onmessage = function(event) {
      var n = parseInt(event.data);
        var total=0;
        for(var i=0; i< n; i++){
      	 total+=i;
        }
        postMessage(total);
        return;
      };
    }//end thread code
    
    
    
    fib=execute(worker_fib, function(a){alert(a);}, true );
    fib(5) //alerts: 10
    fib(2999999998) // alerts: 4499999991067114000 (takes a noticeable amount of time)
    */
    see how even though the bigger number takes a while to calc, you can still hover over the browser menus, ad well as any on-page interaction, and everything works smoothly as though nothing is happening at all.

    if you just ran that in the main thread, you would probably see the "this page is not responding" warning.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #5
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Maybe it's just my environment (Alienware _after_ Dell acquired them), but I've got one Worker that is calculating the prime numbers up to ten million, not even doing a postMessage for each one, just concatenating them into a string and then sending the string via postMessage. Once I trigger it, FF becomes intermittently unresponsive, even after all the numbers are displayed on the page. Chrome is a little better about it.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •