Abz*_*zac 4 python multithreading cross-platform python-2.7
我正在用Python编写程序,它可以在某种沙箱中运行不受信任的python代码.所以,我需要一种方法来限制不受信任的代码可以分配的内存量.现在我可以通过覆盖沙盒环境中的默认python数据结构来限制range(),list,dictionary和其他的最大长度.
有任何想法吗?
在Unix下,您可以使用resource.setrlimit(resource.RLIMIT_AS,...)来限制"进程可能占用的地址空间的最大区域(以字节为单位)".
import sys
import resource
soft, hard = 10**7, 10**7
# soft, hard = 10**8, 10**8 # uncommenting this allows program to finish
resource.setrlimit(resource.RLIMIT_AS,(soft, hard))
memory_hog = {}
try:
for x in range(10000):
print(x)
memory_hog[str(x)]='The sky is so blue'
except MemoryError as err:
sys.exit('memory exceeded')
# memory exceeded
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3805 次 |
| 最近记录: |