In a Python script, I want to set a memory limit for a certain function call. I looked at how to limit heap size; however, I don't want to limit the memory of the entire running Python process -- i.e. setting the memory limit before and after the function call.
在Python脚本中,我想为某个函数调用设置一个内存限制。我研究了如何限制堆大小;但是,我不想限制整个正在运行的Python进程的内存——例如,在函数调用之前和之后设置内存限制。
Is there any way to make a function call with a given amount of memory, so that the memory limit doesn't affect the caller?
是否有办法用给定的内存量进行函数调用,以便内存限制不会影响调用者?
1 个解决方案
#1
2
No, this is impossible. Python doesn't keep track of which functions are responsible for allocating new memory, and nor does libc, so there's no way to limit memory usage just by a single function.
不,这是不可能的。Python没有跟踪哪些函数负责分配新内存,libc也没有,因此没有办法仅通过一个函数来限制内存的使用。
In order to do this you would have to modify Python so that you use a new function for allocating memory that requires it to be specified what Python function is responsible, so that it can reject the memory allocation if that function goes over its limit.
为了做到这一点,您必须修改Python,以便使用一个新的函数来分配内存,该函数需要指定Python函数负责的内容,以便当该函数超出其极限时,它可以拒绝内存分配。
The only other way to do this would indeed be to execute the function in a separate process with limited memory, as @JBernardo said.
另一种方法是在内存有限的单独进程中执行函数,@JBernardo说。
As for implementing sandboxes, there already are relatively well tested implementations. Is there any reason you can't use those? In particular see PyPy's sandboxed VM and the Zope sandbox.
至于实现沙箱,已经有了经过相对良好测试的实现。你有什么理由不能用它们吗?特别是PyPy的沙箱VM和Zope沙箱。
#1
2
No, this is impossible. Python doesn't keep track of which functions are responsible for allocating new memory, and nor does libc, so there's no way to limit memory usage just by a single function.
不,这是不可能的。Python没有跟踪哪些函数负责分配新内存,libc也没有,因此没有办法仅通过一个函数来限制内存的使用。
In order to do this you would have to modify Python so that you use a new function for allocating memory that requires it to be specified what Python function is responsible, so that it can reject the memory allocation if that function goes over its limit.
为了做到这一点,您必须修改Python,以便使用一个新的函数来分配内存,该函数需要指定Python函数负责的内容,以便当该函数超出其极限时,它可以拒绝内存分配。
The only other way to do this would indeed be to execute the function in a separate process with limited memory, as @JBernardo said.
另一种方法是在内存有限的单独进程中执行函数,@JBernardo说。
As for implementing sandboxes, there already are relatively well tested implementations. Is there any reason you can't use those? In particular see PyPy's sandboxed VM and the Zope sandbox.
至于实现沙箱,已经有了经过相对良好测试的实现。你有什么理由不能用它们吗?特别是PyPy的沙箱VM和Zope沙箱。