Setting stacksize in a python script

前端 未结 4 1140
梦谈多话
梦谈多话 2020-11-27 07:05

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 un

相关标签:
4条回答
  • 2020-11-27 07:18

    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.

    0 讨论(0)
  • 2020-11-27 07:19

    You're looking for the Python setrlimit interface, resource.RLIMIT_STACK.

    Note that standard users cannot raise their hard limits, only root (well, a process with the CAP_SYS_RESOURCE capability (see capabilities(7)) processes can raise their limits; so you may need to use the PAM pam_limits(8) limits.conf(5) file to raise the hard limits for the users in question.

    0 讨论(0)
  • 2020-11-27 07:31

    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))
    
    0 讨论(0)
  • 2020-11-27 07:41

    You can alter the stack size of the current process via threading.stack_size(), but I don't know if that will be correctly inherited by subprocesses. That interface also requires a specific stack size - "unlimited" isn't an option.

    0 讨论(0)
提交回复
热议问题