Python ValueError: not allowed to set maximum limit

I am using python 2.7.2 on mac os 10.7.3

I am executing a recursive algorithm in python with over 50,000 recursion levels.

I tried to increase the maximum recursion level to 1,000,000, but my python shell still exits after 18,000 recursion levels.

I tried to increase available resources:

import resource resource.setrlimit(resource.RLIMIT_STACK, (2**29,-1)) sys.setrecursionlimit(10**6) 

and I get this error:

 Traceback (most recent call last): File "<pyshell#58>", line 1, in <module> resource.setrlimit(resource.RLIMIT_STACK,(2**29,-1)) ValueError: not allowed to raise maximum limit 

I don’t know why I can’t increase the maximum limit?

Thanks for your suggestions.

+7
source share
2 answers

From the python documentation:

Raises a ValueError if an invalid resource is specified, if the new soft limit exceeds the hard limit, or if the process tries to raise its limit (unless this process has an effective superuser UID). It may also cause an error if the underlying system call fails.

From this, I assume that your attempt at a new soft restraint is too great. You probably need to rewrite the algorithm to iterative. Python is not intended to handle massive recursion.

+6
source

Although it is probably best to write a more efficient algorithm, you can raise the hard limit by running python as root (as mentioned in the docs).

If you are running as root , you can set the stack size unlimited with the following line:

 import resource resource.setrlimit(resource.RLIMIT_STACK, (resource.RLIM_INFINITY, resource.RLIM_INFINITY)) 
+1
source

All Articles