在python脚本中设置stacksize [英] Setting stacksize in a python script

查看:133
本文介绍了在python脚本中设置stacksize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将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脚本中的调用指定stacksize?我应该使用一些底层系统调用吗?

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?

推荐答案

我对以下代码有很好的经验。它不需要任何特殊的用户权限:

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)

但是它似乎不适用于pypy。

It does however not seem to work with pypy.

这篇关于在python脚本中设置stacksize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆