John Jaques
John Jaques

Reputation: 560

Python 'resource' module - Negative values and unrecognised RLIMIT_VMEM

I am using the Python resource module in order to limit my memory usage, in the following way:

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

However, I encountered the following problems:

  1. The current hard limit is -1. What is the meaning of this value? The problem is that, because it is negative, I cannot set the hard limit to anything higher, and I get an error message (ValueError: current limit exceeds maximum limit).
  2. Contrary to the documentation, the resource module has no RLIMIT_VMEM. When trying to access resource.RLIMIT_VMEM, I get an error (AttributeError: 'module' object has no attribute 'RLIMIT_VMEM'). Could this be caused by some compatibility issues with my OS?

Upvotes: 1

Views: 2414

Answers (2)

Nabeel Ahmed
Nabeel Ahmed

Reputation: 19262

Answer for part 2: the resource module docs mention this possibility:

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 bash ulimit source linked to above, it uses RLIMIT_AS if RLIMIT_VMEM is not defined.

Source: Limit memory usage?

Upvotes: 1

Hiebert
Hiebert

Reputation: 76

If you read the man page for the getrlimit() Linux C call and the prlimit command line tool, they vaguely allude to the fact that -1 is the value of the constant RLIM_INFINITY.

You can verify this in Python.

>>> resource.RLIM_INFINITY
-1

So, essentially you're trying to set a soft limit of Infinity and a hard limit of less than that. The hard limit must be greater than or equal to the soft limit. So instead you can do something like this which should do the trick.

hard_limit = 5 * 1024 * 1024
resource.setrlimit(rsrc, (hard_limit, hard_limit))

As for your second question, I'd love to know the answer, which is what brought me to this question :)

Upvotes: 2

Related Questions