python 资源的软硬限制

Soft, hard limit in python's Resource

python 的 resourcesofthard 限制之间的实际区别是什么?

例如,这样做有什么区别:

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))

最后,是的,我已经阅读了关于软限制和硬限制的文档,但仍然不明白实际的区别是什么:

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.)

CPython 的 resource 显然使用 setrlimit from sys/resource. Looking through GNU's libc manual,它对当前(软)和硬限制有这样的说法:

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.

所以 tl;dr:软是因为一个进程可以增加它的限制,硬是因为它不能,区别在你添加的文档的最后一个带括号的句子中说明:

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