nodejs中有限的函数调用同一个操作?

我目前正在进行一些科学计算,对于这些计算,只要至less有一个参数是错误的,我的基计算循环就会一遍又一遍地执行recursion调用。

目前我的nodejs服务器停止在大约905-915recursion函数调用。

奇怪的是,它不会崩溃,也不会输出任何错误。 它只是停止做任何事情 – >没有更多的日志等

这是从节点的一些保护行为,以避免溢出?

现在我正在为此苦苦挣扎几周,同时试图用尽可能智能的软件来限制“循环”。

感谢您的帮助和build议。 问候Noa。

根据要求,我提供了一些我的实际代码的抽象

我希望这有帮助。 我不能把我原来的代码放在这里,因为它包含了超过1.5万行 – 很多东西需要检查。 但是下面的例子涵盖了recursion调用背后的基本逻辑。

// Contains objects which contain an array // which represents the amount of the ex_obj terms var amount = { a:[10,10], b:[7.5,7.5], c:[2.5,2.5,2.5,2.5] } // Contains objects, which contain an array of other objects // that represent some selection // Each object in an array consists of different values per attribute per 1 amount var ex_obj = { a: [ {aa: 1.41, ab: 0.143, ac: 0.5}, {aa: 1.30, ab: 1.43, ac: 0.42} ], b: [ {aa: 0.32, ab: 5.54, ac: 1.3}, {aa: 0.33, ab: 4.49, ac: 2.5} ], c: [ {aa: 0.54, ab: 1.4, ac: 3.5}, {aa: 0.39, ab: 1.434, ac: 3.91}, {aa: 0.231, ab: 1.44324, ac: 2.91}, {aa: 0.659, ab: 1.554, ac: 3.9124}, ] } // Here we have an object that represents // the "to be" state which should be achieved var should_be ={ aa: 14.534, ab: 3.43, ac: 5.534 } function calculate(){ // Now we want to mulitply the amount object values with // the ex_obj values for(let prop in ex_obj){ for(let i = 0, i < ex_obj[prop].length, i++){ for(let propa in ex_obj[prop][i]){ // here every aa,ab,ac gets mulitplied with the // contains of the amount obj for the matching // propertyname } } } // the next step is to check if the sum of all ex_obj property // child values per aa, ab and ac match the should_be propertie values // if everything is above the should_be and not too high then the // programm can stop here and print out the amount obj. // if the sum of the ex_obj properties is too little // compared to the should_be obj // we need to check which property is too little // eg aa is too little // then we check where aa in the ex_obj per 1 value is // the highest // then we increment the matching amount property child // and start calculate() again // same procedure for - if something is too much } 

由于你的代码不完整,很难说出错的原因。 如果超出节点的调用堆栈限制,则会发生exception,但1000次recursion通常不成问题。

这可能是你窒息了Node.js事件循环。 而不是直接调用recursion函数,你可以尝试调用

 process.nextTick(function(){calculate()}) 

直接recursion调用会导致堆栈溢出,这并不令人惊讶,但是,似乎频繁的函数调用会让节点挂断(顺便说一句,我不知道为什么会发生这种情况:()。

例如,下面的脚本在1.6 GHz CPU和3.3 GiB内存的机器上被冻结在4k〜5k左右的循环中,然后节点继续吞下我的可用内存。

 var i = 0; function cal() { console.log(++i); process.nextTick(cal); } cal(); 

但如果我改变了process.nextTick(cal); setTimeout(cal, 0); ,一切工作正常。

至于你的问题,我认为你可以在你的脚本中使用像setTimeout(calculate, 100) ,以防止recursion调用,并稍微推迟。