繁体   English   中英

javascript 停止无限循环

[英]javascript stop an infinite loop

这是 node.js。

我有一个 function,如果满足几个条件,它可能会变成无限循环。 不受信任的用户设置了这些条件,因此为了这个问题的目的,请假设无限循环是无法修复的。

我仍然需要一种方法来停止无限循环。

这是我正在尝试做的一些示例代码:

var infiniteloop = false;
var condition = true
function loop () {
  while (condition) {
    console.log('hi')
    if (infiniteloop) {
      condition = false
      console.log('oh last one')
    }
  }
}

loop()

所以有几个问题基于我正在尝试做的事情。

  1. 如果infiniteloop变量设置为 true,循环就会停止,对吗?
  2. 如何检测无限循环? 每 3 秒检查一次的东西会很好。
  3. 如果infiniteloop循环变量在同一进程中,则它在循环时不能更改。 我必须将变量存储在不同的进程中吗?
  4. 任何检测到无限循环的东西都需要存在于不同的进程中? 理想情况下,相同的过程会很好,但不管用什么?

谢谢你的帮助。

基于其他建议的解决方案:

function Worker()
{
    this.MaxIterations = 1000000;
    this.Enabled = true;    
    this.condition = true;
    this.iteration = 0;
    this.Loop = function()
    {
        if (this.condition 
            && this.Enabled 
            && this.iteration++ < this.MaxIterations)
        {
            console.log(this.iteration);
            setTimeout(this.Loop.bind(this),0);
        }
    };  
    this.Stop = function()
    {
        this.Enabled = false;
    };
}
var w = new Worker();
setTimeout(w.Loop.bind(w), 0);
setTimeout(w.Stop.bind(w), 3000);

不确定这是否是最佳选择,但是应该能按预期工作。

使用setTimeout恢复循环允许主node.js事件循环处理其他事件,例如w.Stop。

在这种情况下,无穷大取决于循环的最大迭代次数。 此代码阻止了JavaScript的单线程性质,因此,除非使用Web Worker,否则您将始终锁定所有内容。 最好不要每隔x秒检查一次,因为此代码无论如何都会阻止执行间隔或超时,而是将其作为循环迭代的最大阈值放入循环本身。

var infiniteloop = false;
var condition = true;
var loopCounter = 1;
var maxLoopIterations = 1000; 
function loop () {
  while (condition) {
    console.log('hi');
    infiniteLoop = (loopCounter >= maxLoopIterations); 
    if (infiniteloop) {
      condition = false;
      console.log('oh last one');
      break;
    }
    loopCounter++;
  }
}

实际上,您不需要停止无限循环。 使用setImmediate

例如:

var immediateId;

function loop () {
    console.log('hi');
    immediateId = setImmediate(loop);
}

loop();

这段代码将一直打个招呼 ,直到您停止它。

//stop the loop:
clearImmediate(immediateId);

为什么使用setImmediate

  1. 内存消耗保持在较低水平,不会引起内存韭葱;
  2. 不会抛出RangeError: Maximum call stack size exceeded
  3. 表现不错;

更进一步,

我创建此模块是为了轻松管理无限循环:

var util = require('util');
var ee = require('events').EventEmitter;

var Forever = function() {
    ee.call(this);
    this.args = [];
};

util.inherits(Forever, ee);

module.exports = Forever;

Forever.prototype.add = function() {
    if ('function' === typeof arguments[0]) {
        this.handler = arguments[0];
        var args = Array.prototype.slice.call(arguments, 1);
        if (args.length > 0) {
            this.args = args;
        }
    } else {
        this.emit('error', new Error('when using add function, the first argument should be a function'));
        return 0;
    }
    return this;
};

Forever.prototype.run = function() {
    var handler = this.handler;
    var args = this.args;
    var that = this;

this._immediateId = setImmediate(function() {
        if (typeof handler === 'function') {

            switch (args.length) {
                // fast cases
                case 0:
                    handler.call(that);
                    that.run();
                    break;
                case 1:
                    handler.call(that, args[0]);
                    that.run();
                    break;
                case 2:
                    handler.call(that, args[0], args[1]);
                    that.run();
                    break;
                    // slower
                default:
                    handler.apply(that, args);
                    that.run();
            }
                } else {
                //no function added
                that.emit('error', new Error('no function has been added to Forever'));
            }
        });
};

Forever.prototype.stop = function() {
    if (this._immediateId !== null) {
        clearImmediate(this._immediateId);
    } else {
        this.emit('error', new Error('You cannot stop a loop before it has been started'));
    }
};

Forever.prototype.onError = function(errHandler) {
    if ('function' === typeof errHandler) {
        this.on('error', errHandler);
    } else {
        this.emit('error', new Error('You should use a function to handle the error'));
    }
    return this;
};

用法示例:

var Forever = require('path/to/this/file');
var f = new Forever();

// function to be runned
function say(content1, content2){
    console.log(content1 + content2);
}

//add function to the loop
//the first argument is the function, the rest are its arguments
//chainable api
f.add(say, 'hello', ' world!').run();

//stop it after 5s
setTimeout(function(){
    f.stop();
}, 5000);

而已。

您可以创建一个子进程(分支)以检查您的实际进程是否正在响应。 如果没有响应,则分叉将向父级发送消息-杀死父级和分叉。 您在本要点中可以看到的与此类似的东西: https : //gist.github.com/kevinohara80/3173692

如果您要使用Express Node.js服务器,则可以尝试中间件harakiri ,它可以满足您的需求。

我想提出我的解决方案。 就我而言,我有几个无限循环( while(true) ),这些无限循环仅由break语句终止。 很难确定是否满足这些break语句的条件,而且该算法不是我自己开发的,因此也不可以选择完全重构。

我喜欢@JasonSebring带有循环计数器的简单解决方案。 在我的情况下,只需为迭代次数设置一个限制就可以了。 但是我不想将所有这些变量都插入到我的代码中,此外,我还需要一个适用于嵌套循环的解决方案。 所以我想出了这个包装函数:

/**
 * Stop potential infinite loops after a certain number of iterations.
 * @param loopLimit - The maximum number of iterations before loop is aborted.
 * @param silentStop - Whether to abort the loop silently or throw an error instead.
 * @param callBack - Function representing the inner code of the loop.
 */
static finiteLoopHelper(loopLimit, silentStop, callBack) {
  let loopCounter = 0;
  let stopLoop = false;

  while (!stopLoop) {
    // Return value from the callback can invoke an early stop, like a break statement.
    stopLoop = callBack();

    loopCounter++;
    if (loopCounter >= loopLimit) {
      stopLoop = true;
      if (!silentStop) {
        throw Error(`Loop aborted after ${loopLimit} iterations.`);
      }
    }
  }
}

用法是这样的:

let someVariable = 0;

finiteLoopHelper(1000, false, () => { // this line replaces "while (true) {"
  someVariable = someCalculation();
  if (someVariable === someCondition) {
    return false; // like continue in a normal loop.
  }
  codeNotExecutedInCaseOfContinue();

  if (someVariable === someOtherCondition) {
    return true; // like break in a normal loop.
  }

  // Return value at the end can be omitted, because without it the function
  // will return undefined which also keeps the loop running.
  // return false;
});

如果循环之前有条件,则必须在arrow函数中检查此条件,并使用return true; 就像上面的例子一样。

由于不公开循环计数器,因此可以嵌套此解决方案,而无需为内部循环的计数器找到不同的变量名。

在 function 中,只需返回 false 或 undefined。

在 function 中手动抛出 new Error("ERROR")。

将 function 设置为在计时器上运行 - var timer = setInterval(FUNCTION, 1000)。 然后清除它停止 - clearInterval(timer)

使用可以终止的工作人员运行脚本。

使用 window.stop() 来阻止页面加载和运行。

仅适用于 NodeJS – 使用 process.abort() 或 process.exit()。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM