在 python 脚本中设置 stacksize
我正在将 csh 脚本转换为 python 脚本。该脚本调用内存密集型可执行文件,该可执行文件需要非常大的堆栈,因此 csh 脚本将堆栈大小设置为无限制:
limit stacksize unlimited
当我尝试在 python 中重现此脚本时,我使用 os.csh 以非常幼稚的方式执行它们。 system,例如:
os.system('some_executable')
但我不知道如何告诉操作系统以无限的堆栈大小运行这些可执行文件。有没有办法在 python 脚本中指定调用的堆栈大小?我应该使用一些低级系统调用吗?是否有一个模块(类似于shutil)来控制这个?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我对以下代码有很好的经验。它不需要任何特殊的用户权限:
但它似乎不适用于 pypy。
如果
resource.setrlimit
不起作用,您还可以尝试使用线程:I have good experience with the following code. It doesn't require any special user permissions:
It does however not seem to work with pypy.
If
resource.setrlimit
doesn't work, you can also try using threads:如果需要,您可以使用 shell 的 (u)limit 命令:
或者(可能更好)使用 resource.setrlimit:
You can just use the (u)limit command of your shell, if you want:
Or (probably better) use resource.setrlimit:
您正在寻找 Python setrlimit 接口,
resource.RLIMIT_STACK
。请注意,标准用户无法提高其硬限制,只有 root(好吧,具有 CAP_SYS_RESOURCE 功能的进程(请参阅功能(7))进程可以提高其限制;因此您可能需要使用 PAM pam_limits (8)
limits.conf(5)
文件来提高相关用户的硬限制。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.您可以通过
threading.stack_size( )
,但我不知道子进程是否会正确继承它。该接口还需要特定的堆栈大小 - “无限”不是一个选项。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.