Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

maximum recursion depth

1 view
Skip to first unread message

Paul

unread,
Jul 21, 2002, 12:19:05 AM7/21/02
to

I am just learning Python and found that the recursion depth is limited.
Which is probably a good thing when you are learning ;-)
The application that I am writing will recurse many times more than the
100(?) limit in python 2.2.

Can I optimize my code so that there will be less recursion? yes, but my
recursion depth will still be over 1000 times...
Can I rewrite my code without recursion? Maybe, but I do not want to.

My question to you: Is there a way to change the limit of the recursion
depth?

I've looked at the command line, searched the FAQ and searched the last 5000
messages in this newsgroup without luck...

Thanks,
Paul

RuntimeError: maximum recursion depth exceeded


Peter Hansen

unread,
Jul 21, 2002, 1:12:38 AM7/21/02
to
Paul wrote:
>
> My question to you: Is there a way to change the limit of the recursion
> depth?

Python 2.2.1 (#34, Apr 9 2002, 19:34:33) [MSC 32 bit (Intel)] on win32
>>> import sys
>>> dir(sys)
[ ...snippety snip, 'setcheckinterval', 'setprofile', 'setrecursionlimit' ... ]
>>> print sys.setrecursionlimit.__doc__
setrecursionlimit(n)

Set the maximum depth of the Python interpreter stack to n. This
limit prevents infinite recursion from causing an overflow of the C
stack and crashing Python. The highest possible limit is platform-
dependent.
>>> sys.setrecursionlimit(2000)
>>>

:-)

Kalle Svensson

unread,
Jul 21, 2002, 12:34:57 AM7/21/02
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[Paul]


> My question to you: Is there a way to change the limit of the recursion
> depth?

You want the function sys.setrecursionlimit. Check the documentation
for more info: http://python.org/doc/current/lib/module-sys.html .

Peace,
Kalle
- --
Kalle Svensson, http://www.juckapan.org/~kalle/
Student, root and saint in the Church of Emacs.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.6 <http://mailcrypt.sourceforge.net/>

iD8DBQE9OjnudNeA1787sd0RAs0/AJ0bpyQOGvDe4U8fBOQayObvWu6kpACfbh4c
t+gydeMyqGgoUF226ELQ4Es=
=ye1N
-----END PGP SIGNATURE-----


Paul

unread,
Jul 21, 2002, 2:00:17 AM7/21/02
to
Thanks Kalle and Peter.

I've added: sys.setrecursionlimit(3000)
and my application is doing what I want it to do :-) (2200 recursions...)
Now I can start optimizing my code.

Paul

"Peter Hansen" <pe...@engcorp.com> wrote in message
news:3D3A42C6...@engcorp.com...

Tim Peters

unread,
Jul 21, 2002, 1:42:13 AM7/21/02
to
[Paul]
> ...

> Can I optimize my code so that there will be less recursion? yes, but my
> recursion depth will still be over 1000 times...
> Can I rewrite my code without recursion? Maybe, but I do not want to.
>
> My question to you: Is there a way to change the limit of the recursion
> depth?

You've been correctly pointed at sys.setrecursionlimit(), but you should be
aware that unless you bought one of those new-fangled machines with infinite
memory <wink>, your program will eventually crash.

Paul

unread,
Jul 21, 2002, 2:45:14 AM7/21/02
to
unfortunately it is not just memory...
it will (did) crash when the stack size is full, which is only a fraction of
the total memory space... right?

python used less than 6MByte (less than 1% of total) when it crashed with a
much higher limit set for the recursion depth.
It took an awful lot of processing time as well... Have to rethink my
strategy here.

P.

"Tim Peters" <tim...@comcast.net> wrote in message
news:mailman.1027230329...@python.org...

Aahz

unread,
Jul 21, 2002, 9:31:58 AM7/21/02
to
In article <ZCq_8.8646$_C2.6...@newsread2.prod.itd.earthlink.net>,

Paul <phsdv....@earthlink.net> wrote:
>
>My question to you: Is there a way to change the limit of the recursion
>depth?

http://www.stackless.com/
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

Project Vote Smart: http://www.vote-smart.org/

Tim Peters

unread,
Jul 21, 2002, 4:26:46 PM7/21/02
to
[Paul, recursing very deeply]

> unfortunately it is not just memory...
> it will (did) crash when the stack size is full, which is only a
> fraction of the total memory space... right?

The size of the stack you get depends on your operating system, and perhaps
also your platform C runtime implementation -- it's not Python's decision.
If you tell us which OS you're using, someone may be able to tell you how to
tell your OS that you want a larger stack. If you're using threads too,
life can get more complicated in a hurry, because most OS thread
implementations give threads relatively tiny stacks by default.

> python used less than 6MByte (less than 1% of total) when it
> crashed with a much higher limit set for the recursion depth.
> It took an awful lot of processing time as well... Have to rethink my
> strategy here.

Function calls in Python aren't cheap either. Keeping a stack or queue of
"work to do" in an iterative algorithm ("a loop") is often much faster.

Paul

unread,
Jul 21, 2002, 6:02:47 PM7/21/02
to
I think I will rewrite my code using loops. This will be possible, I just
have to put my braincells back in active mode :-)
I found that a too deep recursion will slow down executing to much.
For now my program works with the recursion limit set at 3000.
However I can think of situations where this will not be enough.

For the statistics, I am using win2000 + cygwin (i686-pc-cygwin)
Python 2.2 (#1, Dec 31 2001, 15:21:18)
[GCC 2.95.3-5 (cygwin special)] on cygwin

Paul

"Tim Peters" <tim...@comcast.net> wrote in message

news:mailman.1027283309...@python.org...

Fernando Perez

unread,
Jul 21, 2002, 8:07:09 PM7/21/02
to
> I think I will rewrite my code using loops. This will be possible, I just
> have to put my braincells back in active mode :-)
> I found that a too deep recursion will slow down executing to much.
> For now my program works with the recursion limit set at 3000.
> However I can think of situations where this will not be enough.

Recursion is expensive because function calls are expensive (in general,
but especially so in python). Unwinding a stack 3000 layers deep is just
asking for trouble. In most cases recursive algorithms are a good
choice when you know that you won't go too deep, and when the actual
work done at each call is substantial enough that the overhead of
calling a function can be ignored. If either of those conditions isn't
met, it's probably time to rethink the problem.

You may want to look at generators. Since they don't have to build/uwind
the stack, they may fit nicely for certain classes of problems. There's
a very nice article on generators at IBM Developer Works which a quick
google run will point you to.

good luck,

f.

Alex Martelli

unread,
Jul 22, 2002, 2:23:20 AM7/22/02
to
Paul wrote:

> I think I will rewrite my code using loops. This will be possible, I just
> have to put my braincells back in active mode :-)

Yes, any recursion can be systematically removed. Easier in a language
with GOTO, but, worst case, one can use the "trivial form of Jacopini-
Böhm" which shows how to map any flow diagram into a while loop with a
switch as the body (if/elif tree as the body, in Python). Most often,
one can do better in terms of both readability and performance.


Alex

0 new messages