简体   繁体   中英

Python: setting memory limit for a particular function call

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 -- ie setting the memory limit before and after the function call.

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?

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.

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.

The only other way to do this would indeed be to execute the function in a separate process with limited memory, as @JBernardo said.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM