在 python 脚本中设置堆栈大小

2022-01-22 00:00:00 python stack csh

问题描述

我正在将 csh 脚本转换为 python 脚本.该脚本调用了一个需要非常大堆栈的内存密集型可执行文件,因此 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

当我尝试在 python 中重现这个脚本时,我以一种非常幼稚的方式执行它们,使用 os.system,例如:

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')

但我不知道如何告诉操作系统以无限堆栈大小运行这些可执行文件.有没有办法为 python 脚本中的调用指定堆栈大小?我应该使用一些低级系统调用吗?是否有一个模块(类似于shutil)来控制它?

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?


解决方案

如果需要,您可以使用 shell 的 (u)limit 命令:

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

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

或者(可能更好)使用 resource.setrlimit:

Or (probably better) use resource.setrlimit:

resource.setrlimit(resource.RLIMIT_STACK, (resource.RLIM_INFINITY, resource.RLIM_INFINITY))

相关文章