Problem with destroy #2

Closed
zsimpson opened this Issue May 10, 2011 · 6 comments

2 participants

@zsimpson

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.

Run

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

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
Owner

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.

@rwaldron rwaldron was assigned May 10, 2011
@zsimpson

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 rwaldron closed this in e7cfef9 May 11, 2011
@rwaldron
Owner

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

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

@rwaldron
Owner

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

e7cfef9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment