David542
David542

Reputation: 110267

Soft, hard limit in python's Resource

What is the practical difference between the soft and hard limit in python's resource?

For example, what would be the difference between doing:

import resource
soft_limit,hard_limit=resource.getrlimit(resource.RLIMIT_DATA)

# set soft limit
resource.setrlimit(resource.RLIMIT_DATA, (1024,hard_limit))

# set soft and hard limit
resource.setrlimit(resource.RLIMIT_DATA, (1024,1024))

And finally, yes I have read the docs for the soft and hard limit and still don't understand in practical terms what the difference is:

Resources usage can be limited using the setrlimit() function described below. Each resource is controlled by a pair of limits: a soft limit and a hard limit. The soft limit is the current limit, and may be lowered or raised by a process over time. The soft limit can never exceed the hard limit. The hard limit can be lowered to any value greater than the soft limit, but not raised. (Only processes with the effective UID of the super-user can raise a hard limit.)

Upvotes: 4

Views: 2962

Answers (1)

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

Reputation: 160467

CPython's resource apparently uses setrlimit from sys/resource. Looking through GNU's libc manual, it has this to say on the current (soft) and hard limits:

There are two per-process limits associated with a resource:

current limit

The current limit is the value the system will not allow usage to exceed. It is also called the “soft limit” because the process being limited can generally raise the current limit at will.

maximum limit

The maximum limit is the maximum value to which a process is allowed to set its current limit. It is also called the “hard limit” because there is no way for a process to get around it. A process may lower its own maximum limit, but only the superuser may increase a maximum limit.

So tl;dr: soft because a process can increase its limit, hard because it cannot, the difference is stated in the last parenthesized sentence of the docs you added:

(Only processes with the effective UID of the super-user can raise a hard limit.)

Upvotes: 6

Related Questions