Python“resource”模块负值和无法识别的RLIMIT\u VMEM

2024-05-23 18:51:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Pythonresource module来限制内存使用,方法如下:

import resource
rsrc = resource.RLIMIT_AS
soft, hard = resource.getrlimit(rsrc)
resource.setrlimit(rsrc, (soft, 5*1024*1024))  # hard limit = 5GB

但是,我遇到了以下问题:

  1. 当前的hard限制是-1。这个值是什么意思?问题是,因为它是负的,所以我不能将硬限制设置为更高的值,并且会收到一条错误消息(ValueError: current limit exceeds maximum limit)。在
  2. 与文档相反,resource模块没有RLIMIT_VMEM。当试图访问resource.RLIMIT_VMEM时,我得到一个错误(AttributeError: 'module' object has no attribute 'RLIMIT_VMEM')。这可能是由我的操作系统的兼容性问题引起的吗?在

Tags: 方法内存importas错误resourcesoftmodule
2条回答

如果您阅读了getrlimit()linuxc调用和prlimit命令行工具的手册页,它们模糊地暗示了-1是常量RLIM_INFINITY的值。在

您可以在Python中对此进行验证。在

>>> resource.RLIM_INFINITY
-1

所以,本质上你要设置一个无限的软极限和一个小于这个的硬极限。硬限制必须大于或等于软限制。你应该这样做。在

^{pr2}$

关于你的第二个问题,我很想知道答案,这就是我为什么要问这个问题的原因:)

第2部分的答案:resource模块文档提到了这种可能性:

This module does not attempt to mask platform differences — symbols not defined for a platform will not be available from this module on that platform.

According to the bashulimit source linked to above, it uses RLIMIT_AS if RLIMIT_VMEM is not defined.

来源:Limit memory usage?

相关问题 更多 >