rwaldron / jquery-hive

jQuery Plugin for creating and managing web workers across implementations. Includes Hive.Pollen.js - the thread-safe utility library for connecting worker threads to the Hive.
http://weblog.bocoup.com/javascript-web-workers-from-basics-to-jquery-hive-part-i
203 stars 24 forks source link

Problem with destroy #2

Closed zsimpson closed 13 years ago

zsimpson commented 13 years ago

Hi, thanks so much for the Hive API, it's great. I have a problem / question. I'm trying to kill off the workers but it doesn't seem to be working. See the reduced-to-show-the-problem code below:

Thanks for all your effort on this great API and I appreciate any time you might be able to give me on this problem!

--Zack

MAIN.HTML-----------------------------------------------------------------

Open the console.
Press run. See "loop" incrementing.
Press run again. Desired behavior is that the first thread dies but it contines to run and you now see two loops incrementing.

play_worker_test.js-------------------------------------------------------------------------

importScripts('/javascripts/jquery.hive.pollen.js');

$(function( data ) { var loop = 0; while( true ) { for( var i=0; i<500000000; i++ ) { } $.send( { "loop":loop } ); loop++; } });

zsimpson commented 13 years ago

Sorry I didn't use Markdown correctly. Here is the posting hopefully formatted correctly

main.html

<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.js"></script>
<script src="/javascripts/jquery.flash.js"></script>
<script src="/javascripts/jquery.hive.js"></script>
<script type="text/javascript">

    $(document).ready( function() {
        $("#run").click( function() {

            // DELETE the old thread
            // PROBLEM: Acting like this isn't doing anything
            $.Hive.destroy();

            // LAUNCH a new thread
            $.Hive.create({
                count: 1,
                worker: '/javascripts/play_worker_test.js',
                receive: function (data) {
                    console.log( 'RECEIVED MESSAGE - WORKER: #' + data.WORKER_ID + " loop=" + data.loop );
                },  
            });

            $.Hive.get(0).send( "go" );
        });
    });
</script>
<div>
    Open the console.<br />
    Press run.  See "loop" incrementing.<br />
    Press run again.  Desired behavior is that the first thread dies but it contines to run and you
    now see two loops incrementing.
</div>
<button id="run">Run</button>

</html>

And here is the worker play_worker_test.js

importScripts('/javascripts/jquery.hive.pollen.js');

$(function( data ) {
    var loop = 0;
    while( true ) {
        for( var i=0; i<500000000; i++ ) {
        }
        $.send( { "loop":loop } );
        loop++;
    }
});

rwaldron commented 13 years ago

Awesome, very helpful. One thing I want to run by you is that the cost of creating a new worker thread is high and it's generally a good idea to reuse a worker thread if you can ( which works nicely thanks to it's completely asynchronous behaviour).

That being said, it's still a bug if those threads aren't being destroyed.

zsimpson commented 13 years ago

Yeah, I assume it's expensive but I think it is required in this case. I'm writing a learn-to-program site and the code in the thread will be user-specified and thus might have an infinite loop in it. Therefore, I have to kill the thread as I don't see a way to unblock otherwise.

rwaldron commented 13 years ago

Some history: When I first wrote this, worker.prototype.terminate() didn't work (as in, didn't terminate the worker at all) and the fix I used was to destroy references to the worker objects.

zsimpson commented 13 years ago

Are you saying I need to call thread.terminate or are you saying you changed Hive to make that call?

rwaldron commented 13 years ago

The issue auto-closed when I committed a patch with the fix:

https://github.com/rwldrn/jquery-hive/commit/e7cfef9680c335dbfdc8c66a7f68de9c7c15ede9