I'm training a neural network in the browser. I am considering calling a function that could run for an extended period (overnight), and I'd like to be able to terminate the function execution in a non-fatal way (so I don't lose my data) once I'm happy.
I'm looking for a general solution that would allow any looping function to terminate in its current state from the console or a ui element with minimal changes.
I've seen this question but mine is a bit different; it's now 2016 and we have webworkers and post message, which seem like they would provide the tools for a solution.
-
1ES6 generators or ES7 async will be helpfulSLaks– SLaks2016年10月06日 14:33:25 +00:00Commented Oct 6, 2016 at 14:33
-
1do you have any code for what you've tried?jordaniac89– jordaniac892016年10月06日 14:34:33 +00:00Commented Oct 6, 2016 at 14:34
-
1You can press the pause button in the browser debugger sources tab to pause execution.Sharun– Sharun2016年10月06日 14:37:45 +00:00Commented Oct 6, 2016 at 14:37
-
if I under stood it right you want some logic to be triggered and triggering each other with some web frontend..? look at node-reduser3732793– user37327932016年10月06日 14:59:41 +00:00Commented Oct 6, 2016 at 14:59
-
If your function runs for an extended period, I imagine you are either doing huge for-loops or recursive stuff? If so, you can easily realize a pause by checking some global variable before hopping into the next iteration. Your pause action then sets this global variable true, and the recursive/loop function will stop and should save it's already computed data somewhere. Continue would then be to call the same function with the remaining data, and adding the previously computed data to the result.Danmoreng– Danmoreng2016年10月06日 14:59:48 +00:00Commented Oct 6, 2016 at 14:59
1 Answer 1
Well...if you use recursion the pause thing works. But sadly I cannot recommend it, since the call-stack-size will lead to an error with iterations> 13000.
To demonstrate the possibility, I had to include a setTimeout before the next iteration, because else for 10000 the operation is too fast to pause it in between:
var pause = false;
var pause_iteration;
function recursiveLoop(iteration, maximum) {
if (iteration === maximum) {
console.log("Loop finished!");
} else if (pause) {
console.log("Loop paused at: " + iteration);
pause_iteration = iteration;
} else {
var M = Math.sqrt(Math.random());
setTimeout(function () {
recursiveLoop((iteration + 1), maximum)
}, 100);
}
}
recursiveLoop(0, 10000);
// set pause = true at a later point
pause = true;