大家好,又见面了,我是你们的朋友全栈君。
转载自https://pythonhosted.org/psutil/#psutil.STATUS_RUNNING
Warning
This documentation refers to new 2.X version of psutil. Instructions on how to port existing 1.2.1 code are here. Old 1.2.1 documentation is still available here.
psutil documentation
Quick links
About
From project’s home page:
psutil (python system and process utilities) is a cross-platform library for retrieving information on running
processes and
system utilization (CPU, memory, disks, network) in
Python. It is useful mainly for
system monitoring,
profiling and
limiting process resources and
management of running processes. It implements many functionalities offered by command line tools such as:
ps, top, lsof, netstat, ifconfig, who, df, kill, free, nice, ionice, iostat, iotop, uptime, pidof, tty, taskset, pmap. It currently supports
Linux, Windows, OSX, FreeBSD and
Sun Solaris, both
32-bit and
64-bitarchitectures, with Python versions from
2.6 to 3.4 (users of Python 2.4 and 2.5 may use
2.1.3 version).
PyPy is also known to work.
The psutil documentation you’re reading is distributed as a single HTML page.
Processes
Functions
-
psutil.
-
Return a list of current running PIDs. To iterate over all processes process_iter() should be preferred.
pids
(
)
[source]
-
psutil.
-
Check whether the given PID exists in the current process list. This is faster than doing "pid in psutil.pids()" and should be preferred.
pid_exists
(
pid
)
[source]
-
psutil.
-
Return an iterator yielding a Process class instance for all running processes on the local machine. Every instance is only created once and then cached into an internal table which is updated every time an element is yielded. Cached Process instances are checked for identity so that you’re safe in case a PID has been reused by another process, in which case the cached instance is updated. This is should be preferred over psutil.pids() for iterating over processes. Sorting order in which processes are returned is based on their PID. Example usage:
process_iter
(
)
[source]
-
psutil.
-
Convenience function which waits for a list of Process instances to terminate. Return a (gone, alive) tuple indicating which processes are gone and which ones are still alive. The gone ones will have a new returncode attribute indicating process exit status (it may be None). callback is a function which gets called every time a process terminates (a Process instance is passed as callback argument). Function will return as soon as all processes terminate or when timeout occurs. Tipical use case is:
- send SIGTERM to a list of processes
- give them some time to terminate
- send SIGKILL to those ones which are still alive
Example:
wait_procs
(
procs,
timeout=None,
callback=None
)
[source]
Exceptions
-
class
-
Base exception class. All other exceptions inherit from this one.
psutil.
Error
[source]
-
class
-
Raised by Process class methods when no process with the given pid is found in the current process list or when a process no longer exists. “name” is the name the process had before disappearing and gets set only if Process.name() was previosly called.
psutil.
NoSuchProcess
(
pid,
name=None,
msg=None
)
[source]
-
class
-
Raised by Process class methods when permission to perform an action is denied. “name” is the name of the process (may beNone).
psutil.
AccessDenied
(
pid=None,
name=None,
msg=None
)
[source]
-
class
-
Raised by Process.wait() if timeout expires and process is still alive.
psutil.
TimeoutExpired
(
seconds,
pid=None,
name=None,
msg=None
)
[source]
Process class
-
class
-
Represents an OS process with the given pid. If pid is omitted current process pid (os.getpid()) is used. Raise NoSuchProcess if piddoes not exist. When accessing methods of this class always be prepared to catch NoSuchProcess and AccessDenied exceptions.hash() builtin can be used against instances of this class in order to identify a process univocally over time (the hash is determined by mixing process PID and creation time). As such it can also be used with set()s.
Warning
the way this class is bound to a process is uniquely via its PID. That means that if the Process instance is old enough and the PID has been reused by another process in the meantime you might end up interacting with another process. The only exceptions for which process identity is pre-emptively checked (via PID + creation time) and guaranteed are for nice()(set), ionice() (set), cpu_affinity() (set), rlimit() (set), children(), parent(), suspend() resume(), send_signal(), terminate(), and kill()methods. To prevent this problem for all other methods you can use is_running() before querying the process or use process_iter()in case you’re iterating over all processes.
-
pid
-
The process PID.
[source]
-
ppid
-
The process parent pid. On Windows the return value is cached after first call.
(
)
[source]
-
name
-
The process name. The return value is cached after first call.
(
)
[source]
-
exe
-
The process executable as an absolute path. On some systems this may also be an empty string. The return value is cached after first call.
(
)
[source]
-
cmdline
-
The command line this process has been called with.
(
)
[source]
-
create_time
-
The process creation time as a floating point number expressed in seconds since the epoch, in UTC. The return value is cached after first call.
(
)
[source]
-
as_dict
-
Utility method returning process information as a hashable dictionary. If attrs is specified it must be a list of strings reflecting available Process class’s attribute names (e.g. ['cpu_times', 'name']) else all public (read only) attributes are assumed. ad_valueis the value which gets assigned to a dict key in case AccessDenied exception is raised when retrieving that particular process information.
(
attrs=None,
ad_value=None
)
[source]
-
parent
-
Utility method which returns the parent process as a Process object pre-emptively checking whether PID has been reused. If no parent PID is known return None.
(
)
[source]
-
status
-
The current process status as a string. The returned string is one of the psutil.STATUS_* constants.
(
)
[source]
-
cwd
-
The process current working directory as an absolute path.
(
)
[source]
-
username
-
The name of the user that owns the process. On UNIX this is calculated by using real process uid.
(
)
[source]
-
uids
-
The real, effective and saved user ids of this process as a nameduple. This is the same as os.getresuid() but can be used for every process PID.
Availability: UNIX
(
)
[source]
-
gids
-
The real, effective and saved group ids of this process as a nameduple. This is the same as os.getresgid() but can be used for every process PID.
Availability: UNIX
(
)
[source]
-
terminal
-
The terminal associated with this process, if any, else None. This is similar to “tty” command but can be used for every process PID.
Availability: UNIX
(
)
[source]
-
nice
-
Get or set process niceness (priority). On UNIX this is a number which usually goes from -20 to 20. The higher the nice value, the lower the priority of the process.
On Windows this is available as well by using GetPriorityClass and SetPriorityClass and value is one of thepsutil.*_PRIORITY_CLASS constants. Example which increases process priority on Windows:
Starting from Python 3.3 this same functionality is available as os.getpriority() and os.setpriority().
(
value=None
)
[source]
-
ionice
-
Get or set process I/O niceness (priority). On Linux ioclass is one of the psutil.IOPRIO_CLASS_* constants. value is a number which goes from 0 to 7. The higher the value, the lower the I/O priority of the process. On Windows only ioclass is used and it can be set to 2 (normal), 1 (low) or 0 (very low). The example below sets IDLE priority class for the current process, meaning it will only get I/O time when no other process needs the disk:
On Windows only ioclass is used and it can be set to 2 (normal), 1 (low) or 0 (very low).
Availability: Linux and Windows > Vista
(
ioclass=None,
value=None
)
[source]
-
rlimit
-
Get or set process resource limits (see man prlimit). resource is one of the psutil.RLIMIT_* constants. limits is a (soft, hard)tuple. This is the same as resource.getrlimit() and resource.setrlimit() but can be used for every process PID and only on Linux. Example:
Availability: Linux
(
resource,
limits=None
)
[source]
-
io_counters
-
Return process I/O statistics as a namedtuple including the number of read and write operations performed by the process and the amount of bytes read and written. For Linux refer to /proc filesysem documentation. On BSD there’s apparently no way to retrieve bytes counters, hence -1 is returned for read_bytes and write_bytes fields. OSX is not supported.
Availability: all platforms except OSX
(
)
[source]
-
num_ctx_switches
-
The number voluntary and involuntary context switches performed by this process.
(
)
[source]
-
num_fds
-
The number of file descriptors used by this process.
Availability: UNIX
(
)
[source]
-
num_handles
-
The number of handles used by this process.
Availability: Windows
(
)
[source]
-
num_threads
-
The number of threads currently used by this process.
(
)
[source]
-
threads
-
Return threads opened by process as a list of namedtuples including thread id and thread CPU times (user/system).
(
)
[source]
-
cpu_times
-
Return a tuple whose values are process CPU user and system times which means the amount of time expressed in seconds that a process has spent in user / system mode. This is similar to os.times() but can be used for every process PID.
(
)
[source]
-
cpu_percent
-
Return a float representing the process CPU utilization as a percentage. When interval is > 0.0 compares process times to system CPU times elapsed before and after the interval (blocking). When interval is 0.0 or None compares process times to system CPU times elapsed since last call, returning immediately. That means the first time this is called it will return a meaningless 0.0 value which you are supposed to ignore. In this case is recommended for accuracy that this function be called a second time with at least 0.1 seconds between calls. Example:
Note
a percentage > 100 is legitimate as it can result from a process with multiple threads running on different CPU cores.
Warning
the first time this method is called with interval = 0.0 or None it will return a meaningless 0.0 value which you are supposed to ignore.
(
interval=None
)
[source]
-
cpu_affinity
-
Get or set process current CPU affinity. CPU affinity consists in telling the OS to run a certain process on a limited set of CPUs only. The number of eligible CPUs can be obtained with list(range(psutil.cpu_count())).
Availability: Linux, Windows, BSD
Changed in version 2.2.0: added support for FreeBSD
(
cpus=None
)
[source]
-
memory_info
-
Return a tuple representing RSS (Resident Set Size) and VMS (Virtual Memory Size) in bytes. On UNIX rss and vms are the same values shown by ps. On Windows rss and vms refer to “Mem Usage” and “VM Size” columns of taskmgr.exe. For more detailed memory stats use memory_info_ex().
(
)
[source]
-
memory_info_ex
-
Return a namedtuple with variable fields depending on the platform representing extended memory information about the process. All numbers are expressed in bytes.
Linux OSX BSD SunOS Windows rss rss rss rss num_page_faults vms vms vms vms peak_wset shared pfaults text wset text pageins data peak_paged_pool lib stack paged_pool data peak_nonpaged_pool dirty nonpaged_pool pagefile peak_pagefile private Windows metrics are extracted from PROCESS_MEMORY_COUNTERS_EX structure. Example on Linux:
(
)
[source]
-
memory_percent
-
Compare physical system memory to process resident memory (RSS) and calculate process memory utilization as a percentage.
(
)
[source]
-
memory_maps
-
Return process’s mapped memory regions as a list of nameduples whose fields are variable depending on the platform. As such, portable applications should rely on namedtuple’s path and rss fields only. This method is useful to obtain a detailed representation of process memory usage as explained here. If grouped is True the mapped regions with the same path are grouped together and the different memory fields are summed. If grouped is False every mapped region is shown as a single entity and the namedtuple will also include the mapped region’s address space (addr) and permission set (perms). Seeexamples/pmap.py for an example application.
(
grouped=True
)
[source]
-
children
-
Return the children of this process as a list of Process objects, pre-emptively checking whether PID has been reused. If recursive is True return all the parent descendants. Example assuming A == this process:
Note that in the example above if process X disappears process Y won’t be returned either as the reference to process A is lost.
(
recursive=False
)
[source]
-
open_files
-
Return regular files opened by process as a list of namedtuples including the absolute file name and the file descriptor number (on Windows this is always -1). Example:
(
)
[source]
-
connections
-
Return socket connections opened by process as a list of namedutples. To get system-wide connections usepsutil.net_connections(). Every namedtuple provides 6 attributes:
- fd: the socket file descriptor. This can be passed to socket.fromfd() to obtain a usable socket object. This is only available on UNIX; on Windows -1 is always returned.
- family: the address family, either AF_INET, AF_INET6 or AF_UNIX.
- type: the address type, either SOCK_STREAM or SOCK_DGRAM.
- laddr: the local address as a (ip, port) tuple or a path in case of AF_UNIX sockets.
- raddr: the remote address as a (ip, port) tuple or an absolute path in case of UNIX sockets. When the remote endpoint is not connected you’ll get an empty tuple (AF_INET) or None (AF_UNIX). On Linux AF_UNIX sockets will always have this set to None.
- status: represents the status of a TCP connection. The return value is one of the psutil.CONN_* constants. For UDP and UNIX sockets this is always going to be psutil.CONN_NONE.
The kind parameter is a string which filters for connections that fit the following criteria:
Kind value Connections using “inet” IPv4 and IPv6 “inet4” IPv4 “inet6” IPv6 “tcp” TCP “tcp4” TCP over IPv4 “tcp6” TCP over IPv6 “udp” UDP “udp4” UDP over IPv4 “udp6” UDP over IPv6 “unix” UNIX socket (both UDP and TCP protocols) “all” the sum of all the possible families and protocols Example:
(
kind=”inet”
)
[source]
-
is_running
-
Return whether the current process is running in the current process list. This is reliable also in case the process is gone and its PID reused by another process, therefore it must be preferred over doing psutil.pid_exists(p.pid).
Note
this will return True also if the process is a zombie (p.status() == psutil.STATUS_ZOMBIE).
(
)
[source]
-
send_signal
-
Send a signal to process (see signal module constants) pre-emptively checking whether PID has been reused. This is the same as os.kill(pid, sig). On Windows only SIGTERM is valid and is treated as an alias for kill().
(
signal
)
[source]
-
suspend
-
Suspend process execution with SIGSTOP signal pre-emptively checking whether PID has been reused. On UNIX this is the same as os.kill(pid, signal.SIGSTOP). On Windows this is done by suspending all process threads execution.
(
)
[source]
-
resume
-
Resume process execution with SIGCONT signal pre-emptively checking whether PID has been reused. On UNIX this is the same as os.kill(pid, signal.SIGCONT). On Windows this is done by resuming all process threads execution.
(
)
[source]
-
terminate
-
Terminate the process with SIGTERM signal pre-emptively checking whether PID has been reused. On UNIX this is the same as os.kill(pid, signal.SIGTERM). On Windows this is an alias for kill().
(
)
[source]
-
kill
-
Kill the current process by using SIGKILL signal pre-emptively checking whether PID has been reused. On UNIX this is the same as os.kill(pid, signal.SIGKILL). On Windows this is done by using TerminateProcess.
(
)
[source]
-
wait
-
Wait for process termination and if the process is a children of the current one also return the exit code, else None. On Windows there’s no such limitation (exit code is always returned). If the process is already terminated immediately returnNone instead of raising NoSuchProcess. If timeout is specified and process is still alive raise TimeoutExpired exception. It can also be used in a non-blocking fashion by specifying timeout=0 in which case it will either return immediately or raiseTimeoutExpired. To wait for multiple processes use psutil.wait_procs().
(
timeout=None
)
[source]
-
psutil.
Process
(
pid=None
)
[source]
Popen class
-
class
-
A more convenient interface to stdlib subprocess.Popen. It starts a sub process and deals with it exactly as when usingsubprocess.Popen but in addition it also provides all the methods of psutil.Process class in a single interface. For method names common to both classes such as send_signal(), terminate() and kill() psutil.Process implementation takes precedence. For a complete documentation refer to subprocess module documentation.
Note
Unlike subprocess.Popen this class pre-emptively checks wheter PID has been reused on send_signal(), terminate() andkill() so that you don’t accidentally terminate another process, fixing http://bugs.python.org/issue6973.
psutil.
Popen
(
*args,
**kwargs
)
[source]
Constants
-
psutil.
-
A set of strings representing the status of a process. Returned by psutil.Process.status().
STATUS_RUNNING
psutil.
STATUS_SLEEPING
psutil.
STATUS_DISK_SLEEP
psutil.
STATUS_STOPPED
psutil.
STATUS_TRACING_STOP
psutil.
STATUS_ZOMBIE
psutil.
STATUS_DEAD
psutil.
STATUS_WAKE_KILL
psutil.
STATUS_WAKING
psutil.
STATUS_IDLE
psutil.
STATUS_LOCKED
psutil.
STATUS_WAITING
-
psutil.
-
A set of strings representing the status of a TCP connection. Returned by psutil.Process.connections() (status field).
CONN_ESTABLISHED
psutil.
CONN_SYN_SENT
psutil.
CONN_SYN_RECV
psutil.
CONN_FIN_WAIT1
psutil.
CONN_FIN_WAIT2
psutil.
CONN_TIME_WAIT
psutil.
CONN_CLOSE
psutil.
CONN_CLOSE_WAIT
psutil.
CONN_LAST_ACK
psutil.
CONN_LISTEN
psutil.
CONN_CLOSING
psutil.
CONN_NONE
psutil.
CONN_DELETE_TCB
(
Windows
)
psutil.
CONN_IDLE
(
Solaris
)
psutil.
CONN_BOUND
(
Solaris
)
-
psutil.
-
A set of integers representing the priority of a process on Windows (see MSDN documentation). They can be used in conjunction with psutil.Process.nice() to get or set process priority.
Availability: Windows
ABOVE_NORMAL_PRIORITY_CLASS
psutil.
BELOW_NORMAL_PRIORITY_CLASS
psutil.
HIGH_PRIORITY_CLASS
psutil.
IDLE_PRIORITY_CLASS
psutil.
NORMAL_PRIORITY_CLASS
psutil.
REALTIME_PRIORITY_CLASS
-
psutil.
-
A set of integers representing the I/O priority of a process on Linux. They can be used in conjunction with psutil.Process.ionice() to get or set process I/O priority. IOPRIO_CLASS_NONE and IOPRIO_CLASS_BE (best effort) is the default for any process that hasn’t set a specific I/O priority. IOPRIO_CLASS_RT (real time) means the process is given first access to the disk, regardless of what else is going on in the system. IOPRIO_CLASS_IDLE means the process will get I/O time when no-one else needs the disk. For further information refer to manuals of ionice command line utility or ioprio_get system call.
Availability: Linux
IOPRIO_CLASS_NONE
psutil.
IOPRIO_CLASS_RT
psutil.
IOPRIO_CLASS_BE
psutil.
IOPRIO_CLASS_IDLE
-
psutil.
-
Constants used for getting and setting process resource limits to be used in conjunction with psutil.Process.rlimit(). See man prlimitfor futher information.
Availability: Linux
RLIMIT_INFINITY
psutil.
RLIMIT_AS
psutil.
RLIMIT_CORE
psutil.
RLIMIT_CPU
psutil.
RLIMIT_DATA
psutil.
RLIMIT_FSIZE
psutil.
RLIMIT_LOCKS
psutil.
RLIMIT_MEMLOCK
psutil.
RLIMIT_MSGQUEUE
psutil.
RLIMIT_NICE
psutil.
RLIMIT_NOFILE
psutil.
RLIMIT_NPROC
psutil.
RLIMIT_RSS
psutil.
RLIMIT_RTPRIO
psutil.
RLIMIT_RTTIME
psutil.
RLIMIT_RTPRIO
psutil.
RLIMIT_SIGPENDING
psutil.
RLIMIT_STACK
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/130886.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...