检测递归循环 [英] Detecting recursion loops

查看:77
本文介绍了检测递归循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码通过几个函数进行递归循环。由于有问题的I / O输入,这有时导致无限输入。递归和经过Python递归异常的昂贵I / O.

检测递归循环并通过user-Exception(在N传递或某些复杂条件之后)停止它而不传递一个好方法递归计数器参数通过所有函数?


罗伯特

My code does recursion loops through a couple of functions. Due to problematic I/O input this leads sometimes to "endless" recursions and after expensive I/O to the Python recursion exception.
What would be a good method to detect recursion loops and stop it by user-Exception (after N passes or some complex criteria) without passing a recursion counter parameter through all the funcs?

Robert

推荐答案



robert写道:

robert wrote:

我的代码通过几个函数进行递归循环。由于有问题的I / O输入,这有时导致无限输入。递归和经过Python递归异常的昂贵I / O.

检测递归循环并通过user-Exception(在N传递或某些复杂条件之后)停止它而不传递一个好方法通过所有函数的递归计数器参数?
My code does recursion loops through a couple of functions. Due to problematic I/O input this leads sometimes to "endless" recursions and after expensive I/O to the Python recursion exception.
What would be a good method to detect recursion loops and stop it by user-Exception (after N passes or some complex criteria) without passing a recursion counter parameter through all the funcs?



怎么样`sys.setrecursionlimit`?

http://docs.python.org/lib/module-sys.html


- -

HTH,

Rob

What about `sys.setrecursionlimit`?

http://docs.python.org/lib/module-sys.html

--
HTH,
Rob


robert写道:
robert wrote:

我的代码通过几个函数进行递归循环。由于有问题的I / O输入,这有时导致无限输入。递归和经过Python递归异常的昂贵I / O.

检测递归循环并通过user-Exception(在N传递或某些复杂条件之后)停止它而不传递一个好方法通过所有函数的递归计数器参数?
My code does recursion loops through a couple of functions. Due to problematic I/O input this leads sometimes to "endless" recursions and after expensive I/O to the Python recursion exception.
What would be a good method to detect recursion loops and stop it by user-Exception (after N passes or some complex criteria) without passing a recursion counter parameter through all the funcs?



1.您可以在顶部捕获RuntimeError,检查它是否为
递归深度异常,并在以下情况下引发用户异常它是。


2.考虑你是否在不知不觉中试图掩盖一个错误。 ISTM

无论输入有多么困难,你至少应该能够在b * b上取得进展。你得到这个错误是因为,比如说你没有在某个地方递增一个计数器,因此再次使用相同的参数调用一个函数




3.还要考虑是否可以将代码重写为非递归的
。通常当我递归处理输入时,输入是允许任意深度嵌套的
。 (在这种情况下,我认为

任意限制深度将是一个错误。)但是听起来像你输入的
可能本身就是不可嵌套的。如果是这种情况,

可能完全摆脱递归,或者至少在
处进行错误检查以检测何时输入为无效

深度。


4.如果这些问题不适用,你确实需要检测递归,

我建议使用全局字典来跟踪函数调用。如果你想要跟踪一个函数parse_something,你可以

定义一个这样的字典:


_call_count = {parse_something:0}


并更改parse_something以调整自己的计数器:


def parse_something():

_call_count [parse_something] + = 1

check_invalid_recursion()

....

_call_count [parse_something] - = 1


(你可以定义一个装饰器来更容易地做到这一点;那就是

练习。)


check_invalid_recursion()函数会检查_call_count和

根据你想要的任何条件引发异常。


5.在CPython中,你可以只看到堆栈框架和寻找

重复的函数调用。请参阅sys._getframe的文档。

Carl Banks


1. You could catch RuntimeError at the top, check whether it''s
recursion depth exception, and raise a user exception if it is.

2. Consider whether you''re unwittingly trying to cover up a bug. ISTM
no matter how problematic the input is, you should at least be able to
make progress on it. Are you getting this error because, say, you''re
not incrementing a counter somewhere, and thus recalling a function
with the same arguments again?

3. Also consider whether you could rewrite the code to be
non-recursive. Usually when I process input recursively, the input is
allowed to be arbitrarily deeply nested. (In that case I think it
would be a mistake to arbitrarily limit depth.) But it sounds to me
like your input might be inherently non-nestable. If that''s the case,
it might be possible to get rid of the recursion altogether, or at
least to put in error checking that detects when input is at an invalid
depth.

4. If those concerns don''t apply, and you do need to detect recursion,
I''d suggest using a global dictionary to track function calls. If you
have a function parse_something that you want to track, you could
define a dict like this:

_call_count = { parse_something: 0 }

And change parse_something to adjust its own counter:

def parse_something():
_call_count[parse_something] += 1
check_invalid_recursion()
....
_call_count[parse_something] -= 1

(You could define a decorator to do this more easily; that''s left as an
excercise.)

The check_invalid_recursion() function would inspect _call_count and
raise an exception based on any criteria you want.

5. In CPython, you could just inpect the stack frame and look for
duplicated function calls. See the documentation for sys._getframe.
Carl Banks


嗨!


我希望你不要试图找到无限循环,我只是简单地说b / b
误解了你的问题。因为如果你是,那就算了吧(Turing

任何人?)......无限循环是不可能找到的(减去一些,非常

特定的情况)。 />

Cf. http://en.wikipedia.org/wiki/Halting_problem


干杯,


Hugo Ferreira


PS嗯......似乎我真的读错了,因为你定义了你想要停止(在N通过或一些复杂的标准之后)。无论如何我

为后代留下警告:)
Hi!

I hope you are not trying to find infinite loops and I simply
misunderstood your question. Because if you are, then forget it (Turing
anyone?)... Infinite loops are impossible to find (minus some few, very
specific situations).

Cf. http://en.wikipedia.org/wiki/Halting_problem

Cheers,

Hugo Ferreira

P.S. Hmmm... It seems I really read it wrong since you define that you
want to stop "(after N passes or some complex criteria)". Anyway I
leave the warning for future generations :)

我的代码通过几个函数进行递归循环。由于有问题的I / O输入,这有时导致无限输入。递归和经过Python递归异常的昂贵I / O.

检测递归循环并通过user-Exception(在N传递或某些复杂条件之后)停止它而不传递一个好方法通过所有函数的递归计数器参数?

Robert
My code does recursion loops through a couple of functions. Due to problematic I/O input this leads sometimes to "endless" recursions and after expensive I/O to the Python recursion exception.
What would be a good method to detect recursion loops and stop it by user-Exception (after N passes or some complex criteria) without passing a recursion counter parameter through all the funcs?

Robert


这篇关于检测递归循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆