API ReferenceExceptionspsutil.NoSuchProcess?(pid, name=None, msg=None) Raised when no process with the given PID is found in the current process list or when a process no longer exists (zombie). psutil.AccessDenied?(pid=None, name=None, msg=None) Raised when permission to perform an action is denied. psutil.TimeoutExpired?(pid=None, name=None) Raised on Process.wait(timeout) if timeout expires and process is still alive. New in 0.2.1 Classespsutil.Process(pid) A class which represents an OS process.
>>> import os, psutil, datetime >>> p = psutil.Process(os.getpid()) >>> p.create_time 1307289803.47 >>> datetime.datetime.fromtimestamp(p.create_time).strftime("%Y-%M-%d %H:%M") '2011-03-05 18:03'
New in 0.2.1
New in 0.2.1
Changed in 2.0: Windows implementation has been rewritten in C for performace and pywin32 extension is no longer required.
New in 0.3.0
>>> import psutil, os >>> p = psutil.Process(os.getpid()) >>> p.status 0 >>> p.status == psutil.STATUS_RUNNING True >>> str(p.status) 'running' >>> New in 2.1
>>> p = psutil.Process(os.getpid()) >>> p.nice 0 >>> p.nice = 10 # set/change process priority >>> p.nice 10 On Windows this is available as well by using GetPriorityClass and SetPriorityClass. psutil.*_PRIORITY_CLASS constants (explained here) can be used in conjunction. Example which increases process priority: >> p.nice = psutil.HIGH_PRIORITY_CLASS New in 2.1
Availability: Windows, Linux, FreeBSD
>>> p.get_io_counters() io(read_count=454556, write_count=3456, read_bytes=110592, write_bytes=0) New in 2.1
New in 2.1
>>> import psutil, os >>> p = psutil.Process(os.getpid()) >>> p.set_ionice(psutil.IOPRIO_CLASS_IDLE) >>> New in 2.1
New in 0.2.1
>>> import psutil, os >>> p = psutil.Process(os.getpid()) >>> # blocking >>> p.get_cpu_percent(interval=1) 2.0 >>> # non-blocking (percentage since last call) >>> p.get_cpu_percent(interval=0) 2.9 >>> Changed in 0.2.0: interval parameter was added
>>> import psutil, os >>> f = open('file.ext', 'w') >>> p = psutil.Process(os.getpid()) >>> p.get_open_files() [openfile(path='/home/giampaolo/svn/psutil/file.ext', fd=3)] Changed in 0.2.1: OSX implementation rewritten in C; no longer requiring lsof.
The kind parameter is a string which filters for connections that fit the following criteria:
Example:>>> p = psutil.Process(1694) >>> p.name 'firefox' >>> p.get_connections() [connection(fd=115, family=2, type=1, local_address=('10.0.0.1', 48776), remote_address=('93.186.135.91', 80), status='ESTABLISHED'), connection(fd=117, family=2, type=1, local_address=('10.0.0.1', 43761), remote_address=('72.14.234.100', 80), status='CLOSING'), connection(fd=119, family=2, type=1, local_address=('10.0.0.1', 60759), remote_address=('72.14.234.104', 80), status='ESTABLISHED'), connection(fd=123, family=2, type=1, local_address=('10.0.0.1', 51314), remote_address=('72.14.234.83', 443), status='SYN_SENT')]On FreeBSD this is implemented by parsing lsof command output. If lsof is not installed on the system NotImplementedError exception is raised and for third party processes (different than os.getpid()) results can differ depending on user privileges. Changed in 0.2.1: OSX implementation rewritten in C; no longer requiring lsof.
Changed in 0.2.0: no longer accepts sig keyword argument - use send_signal() instead.
New in 0.2.1
A more convenient interface to stdlib subprocess.Popen. 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 in a unique interface. For method names common to both classes such as kill() and terminate(), psutil.Process implementation takes precedence. For a complete documentation refers to subprocess module documentation.>>> import psutil >>> from subprocess import PIPE >>> p = psutil.Popen(["/usr/bin/python", "-c", "print 'hi'"], stdout=PIPE) >>> p.name 'python' >>> p.uids user(real=1000, effective=1000, saved=1000) >>> p.username 'giampaolo' >>> p.communicate() ('hi\n', None) >>> p.wait(timeout=2) 0 >>> New in 0.2.1 System related functionsProcessespsutil.get_pid_list() Return a list of current running PIDs. psutil.pid_exists(pid) Check whether the given PID exists in the current process list. This is faster than doing pid in psutil.get_pid_list() and should be preferred. psutil.process_iter() Return an iterator yielding a Process class instances for all running processes on the local machine. This should be preferred over doing for pid in psutil.get_pid_list(): psutil.Process(pid) as it safe from race conditions. CPUpsutil.cpu_percent(interval=0.1, 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. In this case is recommended for accuracy that this function be called with at least 0.1 seconds between calls. >>> # 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=0) 2.9 >>> Changed in 0.2.0: interval parameter was added psutil.cpu_times(percpu=False) Return system CPU times as a namedtuple. Every attribute represents the time CPU has spent in the given mode. - user When percpu is True return a list of nameduples 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. Changed in 0.3.0: percpu parameter was added Memorypsutil.phymem_usage() Return the amount of total, used and free physical memory on the system in bytes plus the percentage usage.>>> psutil.phymem_usage() usage(total=4153868288, used=2854199296, free=1299668992, percent=34.6)New in 0.3.0 psutil.virtmem_usage() Return the amount of total, used and free virtual memory on the system in bytes plus the percentage usage. psutil.cached_phymem() Return the amount of cached memory and physical memory buffers on the system, in bytes. Thet reflects the "cached" and "buffers" columns of free command line utility on Linux. Availability: Linux psutil.avail_phymem() These functions are deprecated by psutil.phymem_usage() and psutil.virtmem_usage(). Use them instead. Deprecated in 0.3.0 Diskspsutil.disk_partitions(all=False) Return all mounted disk partitions as a list of namedtuples including device, mount point and filesystem type, similarly to "df" command on posix. New in 0.3.0 psutil.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. OSError is raised if path does not exist. See examples/disk_usage.py script providing an example usage.>>> psutil.disk_usage('/') usage(total=21378641920, used=4809781248, free=15482871808, percent=22.5) New in 0.3.0 psutil.disk_io_counters(perdisk=False) Return system disk I/O statistics as a namedtuple including the following attributes: If perdisk is True return the same information for every physical disk installed on the system as a dictionary with partition ames as the keys and the namedutuple described above as the values. >>> psutil.disk_io_counters() iostat(read_count=8141, write_count=2431, read_bytes=290203, write_bytes=537676, read_time=5868, write_time=94922) New in 0.4.0 Networkpsutil.network_io_counters(pernic=False) Return network I/O statistics as a namedtuple including the following attributes: 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. >>> psutil.network_io_counters() iostat(bytes_sent=1270374, bytes_recv=7828365, packets_sent=9810, packets_recv=11794) New in 0.4.0 Constantspsutil.TOTAL_PHYMEM The amount of total physical memory on the system, in bytes. psutil.NUM_CPUS The number of CPUs on the system. This is preferable than using os.environ['NUMBER_OF_PROCESSORS'] as it is more accurate and always available. psutil.BOOT_TIME A number indicating the system boot time expressed in seconds since the epoch. New in 0.2.1 psutil.ABOVE_NORMAL_PRIORITY_CLASS 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. New in 0.2.1 psutil.IOPRIO_CLASS_NONE A set of integers representing the I/O priority of a process on Linux. They can be used in conjunction with psutil.Process.get_ionice() and psutil.Process.set_ionice() to get or set process I/O priority. For further information refer to ionice command line utility or ioprio_get system call. New in 0.2.1 psutil.STATUS_RUNNING A set of integers representing the status of a process. To be used in conjunction with psutil.Process.status property. If used with str() return a human-readable status string. New in 0.2.1 | ||||||||||||||||||||||