SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    doubt on worker termination

    hello all!

    have a doubt about Workers. lets consider this snipper:

    Code:
    // client code
    var w = new Worker("myWor.js");
    
    // in the worker
    onmessage = function() {
       // blablabla
    }
    is

    Code:
    w.terminate() // in client code
    the same as

    Code:
    self.close() // in worker ciode
    ????

    I made some tests and it does look like both the instructions terminate the worker which in turn stops answering to messages but I thought to double check with you guys.

    ciao

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by nourdine View Post
    is

    Code:
    w.terminate() // in client code
    the same as

    Code:
    self.close() // in worker ciode
    w.terminate() and self.close() can be compared when powering down a computer.
    w.terminate() is like pulling the plug, and self.close() is like performing a safe shutdown.

    w.terminate() doesn't give the worker any opportunity to complete its operations or to cleanup properly, so it's better to use self.close() as much as you can.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    w.terminate() doesn't give the worker any opportunity to complete its operations or to cleanup properly, so it's better to use self.close() as much as you can.
    because self.close() triggers the onclose event where important pre-shutdown routines can be executed. is that correct?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by nourdine View Post
    because self.close() triggers the onclose event where important pre-shutdown routines can be executed. is that correct?
    It's because you can script the worker to properly complete its work before the self.close() command is invoked.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    It's because you can script the worker to properly complete its work before the self.close() command is invoked.
    thats also true.

    by the way have u managed to have the onclose event to fire at all? it is in the spec and it should be fired when calling close() but it looks like its not working. Or maybe am doing something wrong

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by nourdine View Post
    by the way have u managed to have the onclose event to fire at all? it is in the spec and it should be fired when calling close() but it looks like its not working. Or maybe am doing something wrong
    While I haven't made use of web workers myself, I'm not aware of any bugs relating to the firing of a websocket's onclose event.

    Could it be that you're assuming that the onclose event fires when one of the web workers closes itself? I don't think that is the case.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Could it be that you're assuming that the onclose event fires when one of the web workers closes itself? I don't think that is the case.
    I am indeed assuming exactly that! when else should that event be triggered if not when a worker calls close()?

    cheers

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by nourdine View Post
    I am indeed assuming exactly that! when else should that event being triggered if not when a worker calls close()?
    The web socket's onclose event fires when the web socket is closed. Not one of the workers, but the complete socket itself.

    The worker itself doesn't currently have an onclose event yet on anything but gecko-based web browsers.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    The web socket's onclose event fires when the web socket is closed. Not one of the workers, but the complete socket itself.

    The worker itself doesn't currently have an onclose event yet on anything but gecko-based web browsers.
    ok so to summarize: close() and terminate() do exactly the same thing. Only if u use close() it means u r IN the thread and can therefore do some operations before shutting down which u cant do from OUTSIDE (where u can instead use terminate).

    but technically they do the same thing.

    correct?

    by the way I have an ex colleague from christchurch. howz it going downunder?

  10. #10
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by nourdine View Post
    ok so to summarize: close() and terminate() do exactly the same thing.
    No, but you get on to it further here.

    Quote Originally Posted by nourdine View Post
    Only if u use close() it means u r IN the thread and can therefore do some operations before shutting down which u cant do from OUTSIDE (where u can instead use terminate).

    but technically they do the same thing.
    Landing an aircraft is very different from having jet fighters bomb it out of the sky. Sure, technically the aircraft ends up on the ground in both situations, but one (self.close()) is very much the preferred option over the other (worker.terminate())

    Quote Originally Posted by nourdine View Post
    by the way I have an ex colleague from christchurch actually. howz it going downunder?
    We're still shaken. It's likely to take a whole generation for the place to return to mostly it was. Not in 5 years, some in 10, many parts in 15, most in 20.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  11. #11
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    We're still shaken. It's likely to take a whole generation for the place to return to mostly it was. Not in 5 years, some in 10, many parts in 15, most in 20.
    shame man! oh well better than the poor japan I suppose ... good luck anyway kiwiland!


Tags for this Thread

Bookmarks

Posting Permissions

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