Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting stacksize in a python script

Tags:

python

stack

csh

I am converting a csh script to a python script. The script calls a memory-intensive executable which requires a very large stack, so the csh script sets the stacksize to unlimited:

limit stacksize unlimited 

When I try to reproduce this script in python, I execute them in a very naive manner, using os.system, e.g.:

os.system('some_executable') 

But I do not know how to tell the OS to run these executables with unlimited stacksize. Is there a way to specify stacksize for calls within a python script? Is there some low-level system call that I should be using? And is there a module (similar to shutil) which controls this?

like image 822
marshall.ward Avatar asked Feb 21 '11 01:02

marshall.ward


2 Answers

I have good experience with the following code. It doesn't require any special user permissions:

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

It does however not seem to work with pypy.

like image 88
Thomas Ahle Avatar answered Sep 23 '22 16:09

Thomas Ahle


You can just use the (u)limit command of your shell, if you want:

os.system('ulimit -s unlimited; some_executable') 

Or (probably better) use resource.setrlimit:

resource.setrlimit(resource.RLIMIT_STACK, (resource.RLIM_INFINITY, resource.RLIM_INFINITY)) 
like image 20
Nicholas Riley Avatar answered Sep 23 '22 16:09

Nicholas Riley