Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ValueError: not allowed to raise maximum limit

I'm using python 2.7.2 on mac os 10.7.3

I'm doing a recursive algorithm in python with more than 50 000 recursion levels.

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

I tried to increase the resources available :

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 cannot raise the maximum limit ?

thanks for your suggestions .

like image 645
Ricky Bobby Avatar asked Apr 14 '12 16:04

Ricky Bobby


2 Answers

From the python documentation:

Raises ValueError if an invalid resource is specified, if the new soft limit exceeds the hard limit, or if a process tries to raise its hard limit (unless the process has an effective UID of super-user). Can also raise error if the underlying system call fails.

From this I guess that your attempt new soft limit is too large. You probably need to rewrite you algorithm to be iterative. Python's not really designed to handle massive recursion like this.

like image 186
Winston Ewert Avatar answered Nov 20 '22 16:11

Winston Ewert


While it is probably a better idea to write a more effecient algo, you can raise the hard limit by running python as root (as is mentioned in the docs).

If you execute as root, you can actually set the stack size to unlimited with the following line :

import resource
resource.setrlimit(resource.RLIMIT_STACK, (resource.RLIM_INFINITY, resource.RLIM_INFINITY))
like image 3
Hemanshu Vadehra Avatar answered Nov 20 '22 17:11

Hemanshu Vadehra