resource
— Resource usage information资源使用情况信息¶
This module provides basic mechanisms for measuring and controlling system resources utilized by a program.该模块提供了用于测量和控制程序所使用的系统资源的基本机制。
Symbolic constants are used to specify particular system resources and to request usage information about either the current process or its children.符号常量用于指定特定的系统资源,并请求有关当前进程或其子进程的使用信息。
An 系统调用失败时引发OSError
is raised on syscall failure.OSError
。
Resource Limits资源限制¶
Resources usage can be limited using the 可以使用下面描述的setrlimit()
function described below. setrlimit()
函数来限制资源的使用。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.)(只有具有超级用户有效UID的进程才能提高硬限制。)
The specific resources that can be limited are system dependent. 可以限制的特定资源取决于系统。They are described in the getrlimit(2) man page. 它们在getrlimit(2)手册页中进行了描述。The resources listed below are supported when the underlying operating system supports them; resources which cannot be checked or controlled by the operating system are not defined in this module for those platforms.当底层操作系统支持下列资源时,这些资源将得到支持;无法由操作系统检查或控制的资源没有在该模块中为这些平台定义。
-
resource.
RLIM_INFINITY
¶ Constant used to represent the limit for an unlimited resource.用于表示无限资源的限制的常数。
-
resource.
getrlimit
(resource)¶ Returns a tuple返回一个(soft, hard)
with the current soft and hard limits of resource.(soft, hard)
,其中包含当前资源的软限制和硬限制。Raises如果指定了无效资源,则引发ValueError
if an invalid resource is specified, orerror
if the underlying system call fails unexpectedly.ValueError
;如果基础系统调用意外失败,则引发error
。
-
resource.
setrlimit
(resource, limits)¶ Sets new limits of consumption of resource.设置新的resource消耗限制。The limits argument must be a tuplelimits参数必须是描述新极限的两个整数的元组(soft, hard)
of two integers describing the new limits.(soft, hard)
。A value ofRLIM_INFINITY
can be used to request a limit that is unlimited.RLIM_INFINITY
的值可用于请求不受限制的限制。Raises如果指定了无效资源、新的软限制超过了硬限制,或者进程试图提高其硬限制,则引发ValueError
if an invalid resource is specified, if the new soft limit exceeds the hard limit, or if a process tries to raise its hard limit.ValueError
。Specifying a limit of当资源的硬限制或系统限制不是无限的时,指定RLIM_INFINITY
when the hard or system limit for that resource is not unlimited will result in aValueError
.RLIM_INFINITY
的限制将导致ValueError
。A process with the effective UID of super-user can request any valid limit value, including unlimited, but具有超级用户有效UID的进程可以请求任何有效的限制值,包括无限值,但如果请求的限制超过系统施加的限制,仍会引发ValueError
will still be raised if the requested limit exceeds the system imposed limit.ValueError
。如果底层系统调用失败,setrlimit
may also raiseerror
if the underlying system call fails.setrlimit
也可能引发error
。VxWorks only supports settingVxWorks仅支持设置RLIMIT_NOFILE
.RLIMIT_NOFILE
。Raises an auditing event使用参数resource.setrlimit
with argumentsresource
,limits
.resource
,limits
引发审核事件resource.setrlimit
。
-
resource.
prlimit
(pid, resource[, limits])¶ Combines将setrlimit()
andgetrlimit()
in one function and supports to get and set the resources limits of an arbitrary process.setrlimit()
和getrlimit()
组合在一个函数中,并支持获取和设置任意进程的资源限制。If pid is 0, then the call applies to the current process.如果pid为0,则该调用将应用于当前进程。resource and limits have the same meaning as inresource和limits的含义与setrlimit()
, except that limits is optional.setrlimit()
中的含义相同,只是limits是可选的。When limits is not given the function returns the resource limit of the process pid. When limits is given the resource limit of the process is set and the former resource limit is returned.当没有给定limits时,函数返回进程pid的resource限制。当给定限制时,将设置进程的resource限制,并返回以前的资源限制。Raises当找不到pid时引发ProcessLookupError
when pid can’t be found andPermissionError
when the user doesn’t haveCAP_SYS_RESOURCE
for the process.ProcessLookupError
,当用户没有进程的CAP_SYS_RESOURCE
时引发PermissionError
。Raises an auditing event使用参数resource.prlimit
with argumentspid
,resource
,limits
.pid
、resource
、limits
引发审核事件resource.prlimit
。Availability
: Linux 2.6.36 or later with glibc 2.13 or later.:Linux 2.6.36或更高版本,带有glibc 2.13或更高。New in version 3.4.版本3.4中新增。
These symbols define resources whose consumption can be controlled using the 这些符号定义了可以使用下面描述的setrlimit()
and getrlimit()
functions described below. setrlimit()
和getrlimit()
函数控制其消耗的资源。The values of these symbols are exactly the constants used by C programs.这些符号的值正是C程序所使用的常量。
The Unix man page for getrlimit(2) lists the available resources. getrlimit(2)的Unix手册页列出了可用的资源。Note that not all systems use the same symbol or same value to denote the same 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.此模块不试图掩盖平台差异-未为平台定义的符号将无法从该平台上的此模块中获得。
-
resource.
RLIMIT_CORE
¶ The maximum size (in bytes) of a core file that the current process can create.当前进程可以创建的核心文件的最大大小(以字节为单位)。This may result in the creation of a partial core file if a larger core would be required to contain the entire process image.如果需要更大的核心来包含整个过程映像,这可能会导致创建部分核心文件。
-
resource.
RLIMIT_CPU
¶ The maximum amount of processor time (in seconds) that a process can use.进程可以使用的最大处理器时间(以秒为单位)。If this limit is exceeded, a如果超过此限制,则会向进程发送SIGXCPU
signal is sent to the process.SIGXCPU
信号。(See the(有关如何捕获此信号并执行一些有用操作的信息,请参阅signal
module documentation for information about how to catch this signal and do something useful, e.g. flush open files to disk.)signal
模块文档,例如将打开的文件刷新到磁盘。)
-
resource.
RLIMIT_FSIZE
¶ The maximum size of a file which the process may create.进程可以创建的文件的最大大小。
-
resource.
RLIMIT_DATA
¶ The maximum size (in bytes) of the process’s heap.进程堆的最大大小(以字节为单位)。
-
resource.
RLIMIT_STACK
¶ The maximum size (in bytes) of the call stack for the current process.当前进程的调用堆栈的最大大小(以字节为单位)。This only affects the stack of the main thread in a multi-threaded process.这只会影响多线程进程中主线程的堆栈。
-
resource.
RLIMIT_RSS
¶ The maximum resident set size that should be made available to the process.应提供给进程的最大驻留集大小。
-
resource.
RLIMIT_NPROC
¶ The maximum number of processes the current process may create.当前进程可以创建的最大进程数。
-
resource.
RLIMIT_NOFILE
¶ The maximum number of open file descriptors for the current process.当前进程打开的文件描述符的最大数目。
-
resource.
RLIMIT_OFILE
¶ The BSD name forRLIMIT_NOFILE
.RLIMIT_NOFILE
的BSD名称。
-
resource.
RLIMIT_MEMLOCK
¶ The maximum address space which may be locked in memory.可以锁定在内存中的最大地址空间。
-
resource.
RLIMIT_VMEM
¶ The largest area of mapped memory which the process may occupy.进程可能占用的映射内存的最大区域。
-
resource.
RLIMIT_AS
¶ The maximum area (in bytes) of address space which may be taken by the process.进程可能占用的地址空间的最大面积(以字节为单位)。
-
resource.
RLIMIT_MSGQUEUE
¶ The number of bytes that can be allocated for POSIX message queues.可以为POSIX消息队列分配的字节数。Availability: Linux 2.6.8 or later.
New in version 3.4.版本3.4中新增。
-
resource.
RLIMIT_NICE
¶ The ceiling for the process’s nice level (calculated as 20 - rlim_cur).进程良好级别的上限(计算为20-rlim_cur)。Availability: Linux 2.6.12 or later.
New in version 3.4.版本3.4中新增。
-
resource.
RLIMIT_RTPRIO
¶ The ceiling of the real-time priority.
Availability: Linux 2.6.12 or later.
New in version 3.4.版本3.4中新增。
-
resource.
RLIMIT_RTTIME
¶ The time limit (in microseconds) on CPU time that a process can spend under real-time scheduling without making a blocking syscall.在不进行阻塞系统调用的情况下,进程在实时调度下可以花费的CPU时间的时间限制(以微秒为单位)。Availability: Linux 2.6.25 or later.
New in version 3.4.版本3.4中新增。
-
resource.
RLIMIT_SIGPENDING
¶ The number of signals which the process may queue.进程可以排队的信号数量。Availability: Linux 2.6.8 or later.
New in version 3.4.版本3.4中新增。
-
resource.
RLIMIT_SBSIZE
¶ The maximum size (in bytes) of socket buffer usage for this user.此用户的套接字缓冲区使用的最大大小(以字节为单位)。This limits the amount of network memory, and hence the amount of mbufs, that this user may hold at any time.这限制了该用户在任何时候可以持有的网络内存的数量,从而限制了mbufs的数量。Availability: FreeBSD 9 or later.
New in version 3.4.版本3.4中新增。
-
resource.
RLIMIT_SWAP
¶ The maximum size (in bytes) of the swap space that may be reserved or used by all of this user id’s processes.此用户id的所有进程可能保留或使用的交换空间的最大大小(以字节为单位)。This limit is enforced only if bit 1 of the vm.overcommit sysctl is set. Please see tuning(7) for a complete description of this sysctl.只有在设置了vmovercommit sysctl的第1位时,才会强制执行此限制。有关此sysctl的完整描述,请参阅tuning(7)。Availability: FreeBSD 9 or later.
New in version 3.4.版本3.4中新增。
-
resource.
RLIMIT_NPTS
¶ The maximum number of pseudo-terminals created by this user id.此用户id创建的伪终端的最大数量。Availability: FreeBSD 9 or later.
New in version 3.4.版本3.4中新增。
-
resource.
RLIMIT_KQUEUES
¶ The maximum number of kqueues this user id is allowed to create.允许此用户id创建的最大kqueues数。Availability: FreeBSD 11 or later.
New in version 3.10.版本3.10中新增。
Resource Usage资源使用情况¶
These functions are used to retrieve resource usage information:这些函数用于检索资源使用情况信息:
-
resource.
getrusage
(who)¶ This function returns an object that describes the resources consumed by either the current process or its children, as specified by the who parameter.此函数返回一个对象,该对象描述由who参数指定的当前进程或其子进程所消耗的资源。The who parameter should be specified using one of the应该使用下面描述的RUSAGE_*
constants described below.RUSAGE_*
常量之一指定who参数。A simple example:一个简单的例子:from resource import *
import time
# a non CPU-bound task
time.sleep(3)
print(getrusage(RUSAGE_SELF))
# a CPU-bound task
for i in range(10 ** 8):
_ = 1 + 1
print(getrusage(RUSAGE_SELF))The fields of the return value each describe how a particular system resource has been used, e.g. amount of time spent running is user mode or number of times the process was swapped out of main memory.每个返回值的字段都描述了特定系统资源是如何使用的,例如,运行用户模式所花费的时间或进程从主存中交换的次数。Some values are dependent on the clock tick internal, e.g. the amount of memory the process is using.有些值取决于内部时钟刻度,例如进程正在使用的内存量。For backward compatibility, the return value is also accessible as a tuple of 16 elements.为了向后兼容,返回值也可以作为16个元素的元组访问。The fields返回值的字段ru_utime
andru_stime
of the return value are floating point values representing the amount of time spent executing in user mode and the amount of time spent executing in system mode, respectively.ru_utime
和ru_stime
分别是表示在用户模式下执行花费的时间量和在系统模式下执行耗费的时间量的浮点值。The remaining values are integers.其余值为整数。Consult the getrusage(2) man page for detailed information about these values.有关这些值的详细信息,请参阅getrusage(2)手册页。A brief summary is presented here:简要概述如下:Index
Field
Resource
0
ru_utime
time in user mode (float seconds)用户模式下的时间(浮动秒)1
ru_stime
time in system mode (float seconds)系统模式下的时间(浮动秒)2
ru_maxrss
maximum resident set size最大驻留集大小3
ru_ixrss
shared memory size共享内存大小4
ru_idrss
unshared memory size非共享内存大小5
ru_isrss
unshared stack size非共享堆栈大小6
ru_minflt
page faults not requiring I/O不需要I/O的页面故障7
ru_majflt
page faults requiring I/O需要I/O的页面故障8
ru_nswap
number of swap outs交换数量9
ru_inblock
block input operations块输入操作10
ru_oublock
block output operations块输出操作11
ru_msgsnd
messages sent发送的消息12
ru_msgrcv
messages received收到的消息13
ru_nsignals
signals received接收到的信号14
ru_nvcsw
voluntary context switches自愿上下文切换15
ru_nivcsw
involuntary context switches非自愿上下文切换This function will raise a如果指定了无效的who参数,此函数将引发ValueError
if an invalid who parameter is specified.ValueError
。It may also raise在异常情况下,它也可能引发error
exception in unusual circumstances.error
异常。
-
resource.
getpagesize
()¶ Returns the number of bytes in a system page. (This need not be the same as the hardware page size.)返回系统页中的字节数。(这不需要与硬件页面大小相同。)
The following 以下RUSAGE_*
symbols are passed to the getrusage()
function to specify which processes information should be provided for.RUSAGE_*
符号被传递给getrusage()
函数,以指定应为哪些进程提供信息。
-
resource.
RUSAGE_SELF
¶ Pass to传递到getrusage()
to request resources consumed by the calling process, which is the sum of resources used by all threads in the process.getrusage()
以请求调用进程消耗的资源,这是进程中所有线程使用的资源的总和。
-
resource.
RUSAGE_CHILDREN
¶ Pass to传递到getrusage()
to request resources consumed by child processes of the calling process which have been terminated and waited for.getrusage()
以请求已终止并等待的调用进程的子进程所消耗的资源。
-
resource.
RUSAGE_BOTH
¶ Pass to传递到getrusage()
to request resources consumed by both the current process and child processes.getrusage()
以请求当前进程和子进程消耗的资源。May not be available on all systems.可能并非在所有系统上都可用。
-
resource.
RUSAGE_THREAD
¶ Pass to传递到getrusage()
to request resources consumed by the current thread.getrusage()
以请求当前线程所消耗的资源。May not be available on all systems.可能并非在所有系统上都可用。New in version 3.2.版本3.2中新增。