psutil

psutil is a cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network, sensors) in Python. Supported platforms:

  • Linux
  • Windows
  • macOS
  • FreeBSD
  • OpenBSD
  • NetBSD
  • Sun Solaris
  • AIX

Works with Python versions from 2.6 to 3.4+.

Package Contents

STATUS_DEAD = dead
STATUS_DISK_SLEEP = disk-sleep
STATUS_IDLE = idle
STATUS_LOCKED = locked
STATUS_PARKED = parked
STATUS_RUNNING = running
STATUS_SLEEPING = sleeping
STATUS_STOPPED = stopped
STATUS_TRACING_STOP = tracing-stop
STATUS_WAITING = waiting
STATUS_WAKING = waking
STATUS_ZOMBIE = zombie
CONN_CLOSE = CLOSE
CONN_CLOSE_WAIT = CLOSE_WAIT
CONN_CLOSING = CLOSING
CONN_ESTABLISHED = ESTABLISHED
CONN_FIN_WAIT1 = FIN_WAIT1
CONN_FIN_WAIT2 = FIN_WAIT2
CONN_LAST_ACK = LAST_ACK
CONN_LISTEN = LISTEN
CONN_NONE = NONE
CONN_SYN_RECV = SYN_RECV
CONN_SYN_SENT = SYN_SENT
CONN_TIME_WAIT = TIME_WAIT
NIC_DUPLEX_FULL = 2
AIX
BSD
FREEBSD
LINUX
MACOS
NETBSD
OPENBSD
OSX
POSIX
SUNOS
WINDOWS
__version__ = 5.6.0
version_info
POWER_TIME_UNLIMITED
POWER_TIME_UNKNOWN
exception Error(msg='')

Bases:Exception

Base exception class. All other psutil exceptions inherit from this one.

__str__
__repr__(self)
exception NoSuchProcess(pid, name=None, msg=None)

Bases:psutil.Error

Exception raised when a process with a certain PID doesn’t or no longer exists.

exception ZombieProcess(pid, name=None, ppid=None, msg=None)

Bases:psutil.NoSuchProcess

Exception raised when querying a zombie process. This is raised on macOS, BSD and Solaris only, and not always: depending on the query the OS may be able to succeed anyway. On Linux all zombie processes are querable (hence this is never raised). Windows doesn’t have zombie processes.

exception AccessDenied(pid=None, name=None, msg=None)

Bases:psutil.Error

Exception raised when permission to perform an action is denied.

exception TimeoutExpired(seconds, pid=None, name=None)

Bases:psutil.Error

Raised on Process.wait(timeout) if timeout expires and process is still alive.

NoSuchProcess
ZombieProcess
AccessDenied
TimeoutExpired
TimeoutExpired
class Process(pid=None)

Bases:object

Represents an OS process with the given PID. If PID is omitted current process PID (os.getpid()) is used. Raise NoSuchProcess if PID does not exist.

Note that most of the methods of this class do not make sure the PID of the process being queried has been reused over time. That means you might end up retrieving an information referring to another process in case the original one this instance refers to is gone in the meantime.

The only exceptions for which process identity is pre-emptively checked and guaranteed are:

  • parent()
  • children()
  • nice() (set)
  • ionice() (set)
  • rlimit() (set)
  • cpu_affinity (set)
  • suspend()
  • resume()
  • send_signal()
  • terminate()
  • kill()
To prevent this problem for all other methods you can:
  • use is_running() before querying the process
  • if you’re continuously iterating over a set of Process instances use process_iter() which pre-emptively checks

process identity for every yielded instance

__repr__
pid

The process PID.

_init(self, pid, _ignore_nsp=False)
__str__(self)
__eq__(self, other)
__ne__(self, other)
__hash__(self)
oneshot(self)

Utility context manager which considerably speeds up the retrieval of multiple process information at the same time.

Internally different process info (e.g. name, ppid, uids, gids, …) may be fetched by using the same routine, but only one information is returned and the others are discarded. When using this context manager the internal routine is executed once (in the example below on name()) and the other info are cached.

The cache is cleared when exiting the context manager block. The advice is to use this every time you retrieve more than one information about the process. If you’re lucky, you’ll get a hell of a speedup.

>>> import psutil
>>> p = psutil.Process()
>>> with p.oneshot():
...     p.name()  # collect multiple info
...     p.cpu_times()  # return cached value
...     p.cpu_percent()  # return cached value
...     p.create_time()  # return cached value
...
>>>
as_dict(self, attrs=None, ad_value=None)

Utility method returning process information as a hashable dictionary. If attrs is specified it must be a list of strings reflecting available Process class’ attribute names (e.g. [‘cpu_times’, ‘name’]) else all public (read only) attributes are assumed. ad_value is the value which gets assigned in case AccessDenied or ZombieProcess exception is raised when retrieving that particular process information.

parent(self)

Return the parent process as a Process object pre-emptively checking whether PID has been reused. If no parent is known return None.

parents(self)

Return the parents of this process as a list of Process instances. If no parents are known return an empty list.

is_running(self)

Return whether this process is running. It also checks if PID has been reused by another process in which case return False.

ppid(self)

The process parent PID. On Windows the return value is cached after first call.

name(self)

The process name. The return value is cached after first call.

exe(self)

The process executable as an absolute path. May also be an empty string. The return value is cached after first call.

cmdline(self)

The command line this process has been called with.

status(self)

The process current status as a STATUS_* constant.

username(self)

The name of the user that owns the process. On UNIX this is calculated by using real process uid.

create_time(self)

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.

cwd(self)

Process current working directory as an absolute path.

nice(self, value=None)

Get or set process niceness (priority).

uids(self)

Return process UIDs as a (real, effective, saved) namedtuple.

io_counters(self)

Return process I/O statistics as a (read_count, write_count, read_bytes, write_bytes) namedtuple. Those are the number of read/write calls performed and the amount of bytes read and written by the process.

ionice(self, ioclass=None, value=None)

Get or set process I/O niceness (priority).

On Linux ioclass is one of the 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).

Available on Linux and Windows > Vista only.

rlimit(self, resource, limits=None)

Get or set process resource limits as a (soft, hard) tuple.

resource is one of the RLIMIT_* constants. limits is supposed to be a (soft, hard) tuple.

See “man prlimit” for further info. Available on Linux only.

cpu_affinity(self, cpus=None)

Get or set process CPU affinity. If specified, cpus must be a list of CPUs for which you want to set the affinity (e.g. [0, 1]). If an empty list is passed, all egible CPUs are assumed (and set). (Windows, Linux and BSD only).

cpu_num(self)

Return what CPU this process is currently running on. The returned number should be <= psutil.cpu_count() and <= len(psutil.cpu_percent(percpu=True)). It may be used in conjunction with psutil.cpu_percent(percpu=True) to observe the system workload distributed across CPUs.

environ(self)

The environment variables of the process as a dict. Note: this might not reflect changes made after the process started.

num_handles(self)

Return the number of handles opened by this process (Windows only).

num_ctx_switches(self)

Return the number of voluntary and involuntary context switches performed by this process.

num_threads(self)

Return the number of threads used by this process.

threads(self)

Return threads opened by process as a list of (id, user_time, system_time) namedtuples representing thread id and thread CPU times (user/system). On OpenBSD this method requires root access.

children(self, recursive=False)

Return the children of this process as a list of Process instances, pre-emptively checking whether PID has been reused. If recursive is True return all the parent descendants.

Example (A == this process):

A ─┐
│ ├─ B (child) ─┐ │ └─ X (grandchild) ─┐ │ └─ Y (great grandchild) ├─ C (child) └─ D (child)
>>> import psutil
>>> p = psutil.Process()
>>> p.children()
B, C, D
>>> p.children(recursive=True)
B, X, Y, C, D

Note that in the example above if process X disappears process Y won’t be listed as the reference to process A is lost.

cpu_percent(self, interval=None)

Return a float representing the current process CPU utilization as a percentage.

When interval is 0.0 or None (default) compares process times to system CPU times elapsed since last call, returning immediately (non-blocking). That means that the first time this is called it will return a meaningful 0.0 value.

When interval is > 0.0 compares process times to system CPU times elapsed before and after the interval (blocking).

In this case is recommended for accuracy that this function be called with at least 0.1 seconds between calls.

A value > 100.0 can be returned in case of processes running multiple threads on different CPU cores.

The returned value is explicitly NOT split evenly between all available logical CPUs. This means that a busy loop process running on a system with 2 logical CPUs will be reported as having 100% CPU utilization instead of 50%.

Examples:

>>> import psutil
>>> p = psutil.Process(os.getpid())
>>> # blocking
>>> p.cpu_percent(interval=1)
2.0
>>> # non-blocking (percentage since last call)
>>> p.cpu_percent(interval=None)
2.9
>>>
cpu_times(self)

Return a (user, system, children_user, children_system) namedtuple representing the accumulated process time, in seconds. This is similar to os.times() but per-process. On macOS and Windows children_user and children_system are always set to 0.

memory_info(self)

Return a namedtuple with variable fields depending on the platform, representing memory information about the process.

The “portable” fields available on all plaforms are rss and vms.

All numbers are expressed in bytes.

memory_info_ex(self)
memory_full_info(self)

This method returns the same information as memory_info(), plus, on some platform (Linux, macOS, Windows), also provides additional metrics (USS, PSS and swap). The additional metrics provide a better representation of actual process memory usage.

Namely USS is the memory which is unique to a process and which would be freed if the process was terminated right now.

It does so by passing through the whole process address. As such it usually requires higher user privileges than memory_info() and is considerably slower.

memory_percent(self, memtype='rss')

Compare process memory to total physical system memory and calculate process memory utilization as a percentage. memtype argument is a string that dictates what type of process memory you want to compare against (defaults to “rss”). The list of available strings can be obtained like this:

>>> psutil.Process().memory_info()._fields
('rss', 'vms', 'shared', 'text', 'lib', 'data', 'dirty', 'uss', 'pss')
memory_maps(self, grouped=True)

Return process’ mapped memory regions as a list of namedtuples whose fields are variable depending on the platform.

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

open_files(self)

Return files opened by process as a list of (path, fd) namedtuples including the absolute file name and file descriptor number.

connections(self, kind='inet')

Return socket connections opened by process as a list of (fd, family, type, laddr, raddr, status) namedtuples. The kind parameter filters for connections that match the following criteria:

Kind Value Connections using
inet inet4 inet6 tcp tcp4 tcp6 udp udp4 udp6 unix all IPv4 and IPv6 IPv4 IPv6 TCP TCP over IPv4 TCP over IPv6 UDP UDP over IPv4 UDP over IPv6 UNIX socket (both UDP and TCP protocols) the sum of all the possible families and protocols
_send_signal(self, sig)
send_signal(self, sig)

Send a signal sig to process pre-emptively checking whether PID has been reused (see signal module constants) . On Windows only SIGTERM is valid and is treated as an alias for kill().

suspend(self)

Suspend process execution with SIGSTOP pre-emptively checking whether PID has been reused. On Windows this has the effect ot suspending all process threads.

resume(self)

Resume process execution with SIGCONT pre-emptively checking whether PID has been reused. On Windows this has the effect of resuming all process threads.

terminate(self)

Terminate the process with SIGTERM pre-emptively checking whether PID has been reused. On Windows this is an alias for kill().

kill(self)

Kill the current process with SIGKILL pre-emptively checking whether PID has been reused.

wait(self, timeout=None)

Wait for process to terminate and, if process is a children of os.getpid(), also return its exit code, else None.

If the process is already terminated immediately return None instead of raising NoSuchProcess.

If timeout (in seconds) is specified and process is still alive raise TimeoutExpired.

To wait for multiple Process(es) use psutil.wait_procs().

class Popen(*args, **kwargs)

Bases:psutil.Process

A more convenient interface to stdlib subprocess.Popen class.

It starts a sub process and deals with it exactly as when using subprocess.Popen class but in addition also provides all the properties and methods of psutil.Process class as a unified interface:

>>> import psutil
>>> from subprocess import PIPE
>>> p = psutil.Popen(["python", "-c", "print 'hi'"], stdout=PIPE)
>>> p.name()
'python'
>>> p.uids()
user(real=1000, effective=1000, saved=1000)
>>> p.username()
'giampaolo'
>>> p.communicate()
('hi
‘, None)
>>> p.terminate()
>>> p.wait(timeout=2)
0
>>>

For method names common to both classes such as kill(), terminate() and wait(), psutil.Process implementation takes precedence.

Unlike subprocess.Popen this class pre-emptively checks whether PID has been reused on send_signal(), terminate() and kill() so that you don’t accidentally terminate another process, fixing http://bugs.python.org/issue6973.

For a complete documentation refer to: http://docs.python.org/library/subprocess.html

__dir__(self)
__enter__(self)
__exit__(self, *args, **kwargs)
__getattribute__(self, name)
wait(self, timeout=None)
pids()

Return a list of current running PIDs.

pid_exists(pid)

Return True if given PID exists in the current process list. This is faster than doing “pid in psutil.pids()” and should be preferred.

process_iter(attrs=None, ad_value=None)

Return a generator yielding a Process instance for all running processes.

Every new Process instance is only created once and then cached into an internal table which is updated every time this is used.

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.

The sorting order in which processes are yielded is based on their PIDs.

attrs and ad_value have the same meaning as in Process.as_dict(). If attrs is specified as_dict() is called and the resulting dict is stored as a ‘info’ attribute attached to returned Process instance. If attrs is an empty list it will retrieve all process info (slow).

wait_procs(procs, timeout=None, callback=None)

Convenience function which waits for a list of processes 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 (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. Differently from Process.wait() it will not raise TimeoutExpired if timeout occurs.

Typical 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:

>>> def on_terminate(proc):
...     print("process {} terminated".format(proc))
...
>>> for p in procs:
...    p.terminate()
...
>>> gone, alive = wait_procs(procs, timeout=3, callback=on_terminate)
>>> for p in alive:
...     p.kill()
cpu_count(logical=True)

Return the number of logical CPUs in the system (same as os.cpu_count() in Python 3.4).

If logical is False return the number of physical cores only (e.g. hyper thread CPUs are excluded).

Return None if undetermined.

The return value is cached after first call. If desired cache can be cleared like this:

>>> psutil.cpu_count.cache_clear()
cpu_times(percpu=False)

Return system-wide CPU times as a namedtuple. Every CPU time represents the seconds the CPU has spent in the given mode. The namedtuple’s fields availability varies depending on the platform:

  • user
  • system
  • idle
  • nice (UNIX)
  • iowait (Linux)
  • irq (Linux, FreeBSD)
  • softirq (Linux)
  • steal (Linux >= 2.6.11)
  • guest (Linux >= 2.6.24)
  • guest_nice (Linux >= 3.2.0)

When percpu is True return a list of namedtuples for each CPU. First element of the list refers to first CPU, second element to second CPU and so on. The order of the list is consistent across calls.

cpu_percent(interval=None, percpu=False)

Return a float representing the current system-wide CPU utilization as a percentage.

When interval is > 0.0 compares system CPU times elapsed before and after the interval (blocking).

When interval is 0.0 or None compares system CPU times elapsed since last call or module import, returning immediately (non blocking). That means the first time this is called it will return a meaningless 0.0 value which you should ignore. In this case is recommended for accuracy that this function be called with at least 0.1 seconds between calls.

When percpu is True returns a list of floats representing the utilization as a percentage for each CPU. First element of the list refers to first CPU, second element to second CPU and so on. The order of the list is consistent across calls.

Examples:

>>> # blocking, system-wide
>>> psutil.cpu_percent(interval=1)
2.0
>>>
>>> # blocking, per-cpu
>>> psutil.cpu_percent(interval=1, percpu=True)
[2.0, 1.0]
>>>
>>> # non-blocking (percentage since last call)
>>> psutil.cpu_percent(interval=None)
2.9
>>>
cpu_times_percent(interval=None, percpu=False)

Same as cpu_percent() but provides utilization percentages for each specific CPU time as is returned by cpu_times(). For instance, on Linux we’ll get:

>>> cpu_times_percent()
cpupercent(user=4.8, nice=0.0, system=4.8, idle=90.5, iowait=0.0,
           irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
>>>

interval and percpu arguments have the same meaning as in cpu_percent().

cpu_stats()

Return CPU statistics.

virtual_memory()

Return statistics about system memory usage as a namedtuple including the following fields, expressed in bytes:

  • total: total physical memory available.

  • available: the memory that can be given instantly to processes without the system going into swap. This is calculated by summing different memory values depending on the platform and it is supposed to be used to monitor actual memory usage in a cross platform fashion.

  • percent: the percentage usage calculated as (total - available) / total * 100

  • used:

    memory used, calculated differently depending on the platform and designed for informational purposes only: macOS: active + inactive + wired BSD: active + wired + cached Linux: total - free

  • free: memory not being used at all (zeroed) that is readily available; note that this doesn’t reflect the actual memory available (use ‘available’ instead)

Platform-specific fields:

  • active (UNIX): memory currently in use or very recently used, and so it is in RAM.
  • inactive (UNIX): memory that is marked as not used.
  • buffers (BSD, Linux): cache for things like file system metadata.
  • cached (BSD, macOS): cache for various things.
  • wired (macOS, BSD): memory that is marked to always stay in RAM. It is never moved to disk.
  • shared (BSD): memory that may be simultaneously accessed by multiple processes.

The sum of ‘used’ and ‘available’ does not necessarily equal total. On Windows ‘available’ and ‘free’ are the same.

swap_memory()

Return system swap memory statistics as a namedtuple including the following fields:

  • total: total swap memory in bytes
  • used: used swap memory in bytes
  • free: free swap memory in bytes
  • percent: the percentage usage
  • sin: no. of bytes the system has swapped in from disk (cumulative)
  • sout: no. of bytes the system has swapped out from disk (cumulative)

‘sin’ and ‘sout’ on Windows are meaningless and always set to 0.

disk_usage(path)

Return disk usage statistics about the given path as a namedtuple including total, used and free space expressed in bytes plus the percentage usage.

disk_partitions(all=False)

Return mounted partitions as a list of (device, mountpoint, fstype, opts) namedtuple. ‘opts’ field is a raw string separated by commas indicating mount options which may vary depending on the platform.

If all parameter is False return physical devices only and ignore all others.

disk_io_counters(perdisk=False, nowrap=True)

Return system disk I/O statistics as a namedtuple including the following fields:

  • read_count: number of reads
  • write_count: number of writes
  • read_bytes: number of bytes read
  • write_bytes: number of bytes written
  • read_time: time spent reading from disk (in ms)
  • write_time: time spent writing to disk (in ms)

Platform specific:

  • busy_time: (Linux, FreeBSD) time spent doing actual I/Os (in ms)
  • read_merged_count (Linux): number of merged reads
  • write_merged_count (Linux): number of merged writes

If perdisk is True return the same information for every physical disk installed on the system as a dictionary with partition names as the keys and the namedtuple described above as the values.

If nowrap is True it detects and adjust the numbers which overflow and wrap (restart from 0) and add “old value” to “new value” so that the returned numbers will always be increasing or remain the same, but never decrease. “disk_io_counters.cache_clear()” can be used to invalidate the cache.

On recent Windows versions ‘diskperf -y’ command may need to be executed first otherwise this function won’t find any disk.

net_io_counters(pernic=False, nowrap=True)

Return network I/O statistics as a namedtuple including the following fields:

  • bytes_sent: number of bytes sent

  • bytes_recv: number of bytes received

  • packets_sent: number of packets sent

  • packets_recv: number of packets received

  • errin: total number of errors while receiving

  • errout: total number of errors while sending

  • dropin: total number of incoming packets which were dropped

  • dropout: total number of outgoing packets which were dropped

    (always 0 on macOS and BSD)

If pernic is True return the same information for every network interface installed on the system as a dictionary with network interface names as the keys and the namedtuple described above as the values.

If nowrap is True it detects and adjust the numbers which overflow and wrap (restart from 0) and add “old value” to “new value” so that the returned numbers will always be increasing or remain the same, but never decrease. “disk_io_counters.cache_clear()” can be used to invalidate the cache.

net_connections(kind='inet')

Return system-wide socket connections as a list of (fd, family, type, laddr, raddr, status, pid) namedtuples. In case of limited privileges ‘fd’ and ‘pid’ may be set to -1 and None respectively. The kind parameter filters for connections that fit the following criteria:

Kind Value Connections using
inet inet4 inet6 tcp tcp4 tcp6 udp udp4 udp6 unix all IPv4 and IPv6 IPv4 IPv6 TCP TCP over IPv4 TCP over IPv6 UDP UDP over IPv4 UDP over IPv6 UNIX socket (both UDP and TCP protocols) the sum of all the possible families and protocols

On macOS this function requires root privileges.

net_if_addrs()

Return the addresses associated to each NIC (network interface card) installed on the system as a dictionary whose keys are the NIC names and value is a list of namedtuples for each address assigned to the NIC. Each namedtuple includes 5 fields:

  • family: can be either socket.AF_INET, socket.AF_INET6 or

    psutil.AF_LINK, which refers to a MAC address.

  • address: is the primary address and it is always set.

  • netmask: and ‘broadcast’ and ‘ptp’ may be None.

  • ptp: stands for “point to point” and references the

    destination address on a point to point interface (typically a VPN).

  • broadcast: and ptp are mutually exclusive.

Note: you can have more than one address of the same family associated with each interface.

net_if_stats()

Return information about each NIC (network interface card) installed on the system as a dictionary whose keys are the NIC names and value is a namedtuple with the following fields:

  • isup: whether the interface is up (bool)

  • duplex: can be either NIC_DUPLEX_FULL, NIC_DUPLEX_HALF or

    NIC_DUPLEX_UNKNOWN

  • speed: the NIC speed expressed in mega bits (MB); if it can’t

    be determined (e.g. ‘localhost’) it will be set to 0.

  • mtu: the maximum transmission unit expressed in bytes.

boot_time()

Return the system boot time expressed in seconds since the epoch.

users()

Return users currently connected on the system as a list of namedtuples including the following fields.

  • user: the name of the user
  • terminal: the tty or pseudo-tty associated with the user, if any.
  • host: the host name associated with the entry, if any.
  • started: the creation time as a floating point number expressed in seconds since the epoch.