Closed zsimpson closed 13 years ago
Sorry I didn't use Markdown correctly. Here is the posting hopefully formatted correctly
<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>
importScripts('/javascripts/jquery.hive.pollen.js'); $(function( data ) { var loop = 0; while( true ) { for( var i=0; i<500000000; i++ ) { } $.send( { "loop":loop } ); loop++; } });
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.
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.
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.
Are you saying I need to call thread.terminate or are you saying you changed Hive to make that call?
The issue auto-closed when I committed a patch with the fix:
https://github.com/rwldrn/jquery-hive/commit/e7cfef9680c335dbfdc8c66a7f68de9c7c15ede9
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-----------------------------------------------------------------
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++; } });